Skip to content

Commit

Permalink
update Spine
Browse files Browse the repository at this point in the history
  • Loading branch information
maccman committed May 13, 2011
1 parent 5d763a0 commit ae2f2ef
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions lib/spine.js
Expand Up @@ -7,9 +7,9 @@
Spine = this.Spine = {};
}

Spine.version = "0.0.3";
Spine.version = "0.0.4";

var $ = Spine.$ = this.jQuery || this.Zepto;
var $ = Spine.$ = this.jQuery || this.Zepto || function(){ return arguments[0]; };

var makeArray = Spine.makeArray = function(args){
return Array.prototype.slice.call(args, 0);
Expand Down Expand Up @@ -106,7 +106,7 @@
created: function(){},

prototype: {
initializer: function(){},
initialize: function(){},
init: function(){}
},

Expand All @@ -124,12 +124,12 @@
},

init: function(){
var initance = Object.create(this.prototype);
initance.parent = this;
var instance = Object.create(this.prototype);
instance.parent = this;

initance.initializer.apply(initance, arguments);
initance.init.apply(initance, arguments);
return initance;
instance.initialize.apply(instance, arguments);
instance.init.apply(instance, arguments);
return instance;
},

proxy: function(func){
Expand Down Expand Up @@ -194,17 +194,8 @@

created: function(sub){
this.records = {};
this.attributes = [];

this.bind("create", this.proxy(function(record){
this.trigger("change", "create", record);
}));
this.bind("update", this.proxy(function(record){
this.trigger("change", "update", record);
}));
this.bind("destroy", this.proxy(function(record){
this.trigger("change", "destroy", record);
}));
this.attributes = this.attributes ?
makeArray(this.attributes) : [];
},

find: function(id){
Expand Down Expand Up @@ -295,8 +286,7 @@

create: function(atts){
var record = this.init(atts);
record.save();
return record;
return record.save();
},

destroy: function(id){
Expand All @@ -316,15 +306,17 @@
},

fromJSON: function(objects){
var self = this;
if ( !objects ) return;
if (typeof objects == "string")
objects = JSON.parse(objects)
if (typeof objects == "array")
return($.map(objects, function(){
return self.init(this);
}));
else
return this.init(objects);
if (typeof objects.length == "number") {
var results = [];
for (var i=0; i < objects.length; i++)
results.push(this.init(objects[i]));
return results;
} else {
return this.init(objects);
}
},

// Private
Expand Down Expand Up @@ -385,8 +377,8 @@
save: function(){
var error = this.validate();
if ( error ) {
if ( !this.trigger("error", this, error) )
throw("Validation failed: " + error);
this.trigger("error", this, error)
return false;
}

this.trigger("beforeSave", this);
Expand All @@ -409,6 +401,7 @@
this.trigger("beforeDestroy", this);
delete this.parent.records[this.id];
this.trigger("destroy", this);
this.trigger("change", this, "destroy");
},

dup: function(){
Expand Down Expand Up @@ -442,7 +435,9 @@
this.trigger("beforeUpdate", this);
var records = this.parent.records;
records[this.id].load(this.attributes());
this.trigger("update", records[this.id].clone());
var clone = records[this.id].clone();
this.trigger("update", clone);
this.trigger("change", clone, "update");
},

create: function(){
Expand All @@ -451,7 +446,9 @@
this.newRecord = false;
var records = this.parent.records;
records[this.id] = this.dup();
this.trigger("create", records[this.id].clone());
var clone = records[this.id].clone();
this.trigger("create", clone);
this.trigger("change", clone, "create");
},

bind: function(events, callback){
Expand All @@ -473,7 +470,7 @@
var Controller = Spine.Controller = Class.create({
tag: "div",

initializer: function(options){
initialize: function(options){
this.options = options;

for (var key in this.options)
Expand Down

0 comments on commit ae2f2ef

Please sign in to comment.