Skip to content

Instantly share code, notes, and snippets.

@postpostmodern
Forked from wyattdanger/plan.js
Created September 21, 2012 14:22
Show Gist options
  • Save postpostmodern/3761730 to your computer and use it in GitHub Desktop.
Save postpostmodern/3761730 to your computer and use it in GitHub Desktop.
js demo
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