Skip to content

Instantly share code, notes, and snippets.

@k33g
Created July 21, 2011 09:07
Show Gist options
  • Save k33g/1096826 to your computer and use it in GitHub Desktop.
Save k33g/1096826 to your computer and use it in GitHub Desktop.
Species.MVC à la Papa, dédicace à @loic_d
(function () {
var Customer = Species.Class({
Extends : Species.Model,
Name : {
get : function() { return this.name; },
set : function(value) { this.name = value; }
},
Budget : {
get : function() { return this.budget; },
set : function(value) { this.budget = value; }
},
initialize : function _Customer(id, name, budget){
console.log(this.UID);
this.name = name;
this.id = id;
this.budget = budget;
}
});
var Customers = Species.Class({
Extends : Species.ModelHelper,
initialize : function _CustomerHelper() {
this.Model = Customer;
}
});
var CustomerController = Species.Class({
Extends : Species.Controller,
populate : function() {
var toto = Customer.New("001", "TOTO", 45000);
var tutu = Customer.New("002", "TUTU", 3000);
var tata = this.customers.clone(tutu);
tata.Id = "003";
tata.Name = "TATA";
//this.customers.add([toto, tutu, tata]);
this.customers.add(toto);
this.customers.add(tutu);
this.customers.add(tata);
this.customers.save(toto);
this.customers.save(tutu);
this.customers.save(tata);
this.customers.each(function(e) {
$("#customersList").append("<li>" + e.Id + " : " + e.Name + " : " + e.Budget + "</li>");
});
/*this.customers.getAll().forEach(function(e) { console.log( e.Id, e.Name, e.Budget); } );*/
},
initialize : function _CustomerController() {
var that = this;
this.customers = Customers.New(),
this.customers.bind({
method : "add",
before : function(data){ console.log("BEFORE ADD : " + data.Name); } ,
after : function(data){ console.log("AFTER ADD : " + data.Name); }
});
this.customers.bind({
method : "save",
after : function(data){ console.log(data.UID + " : " + data.Name + " ("+ data.Id +") SAVED"); }
})
this.View = $('#customersView'); /* pour le moment pas utilisée */
this.Events = [
{
element : '#cmd01',
event : 'click',
onEvent : function(){
$('result').html(that.customers.find(function(e) { return e.Budget === 3000; } ).Name);
}
},
{
element : '#cmd02',
event : 'click',
onEvent : function(){
$('result').html('');
that.customers.filter(function(e) {
return e.Budget === 3000;
} ).forEach(function(e){
$('result').html(e.Name + ' - ' + $('result').html());
});
}
}
];
this.populate();
}
});
var control = CustomerController.New();
}());
@mklabs
Copy link

mklabs commented Jul 21, 2011

MVC à la Papa @loicdescotte on tiens un truc là!

@k33g
Copy link
Author

k33g commented Jul 21, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment