-
-
Save postpostmodern/3761730 to your computer and use it in GitHub Desktop.
js demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Plan = (function() { | |
// protected | |
var records = []; | |
// the Plan constructor | |
function Plan(key, id, price) { | |
this.id = id; | |
this.price = price; | |
var more = key.split('||'); | |
this.emails = more[0]; | |
this.classification = more[1]; | |
this.period = more[2]; | |
records.push(this); | |
} | |
// Plan instance methods go here | |
Plan.prototype = { | |
formattedPrice: function() { | |
if (isNaN(this.price)) { | |
return this.price; | |
} else { | |
return "$" + this.price + ".00"; | |
} | |
} | |
}; | |
// "Class" methods | |
Plan.find = function(id) { | |
var result = records.filter(function(record) { | |
return record.id === id; | |
}); | |
return result.length ? result[0] : null; | |
}; | |
Plan.all = function() { | |
return records; | |
}; | |
Plan.import = function(plansObj) { | |
for(var key in plansObj) { | |
var planObj = plansObj[key]; | |
new Plan(key, planObj.id, planObj.price); | |
} | |
}; | |
return Plan; | |
}()); | |
// Existing data | |
var plans = { '1000||non-profit||monthly': { id: "222bbb", price: 24 }, '1000||non-profit||yearly': { id: "111aaa", price: 200 } }; | |
Plan.import(plans); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment