Skip to content

Commit

Permalink
update to the latest version of spine
Browse files Browse the repository at this point in the history
  • Loading branch information
maccman committed Apr 4, 2011
1 parent f431f8f commit 9d43499
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 34 deletions.
25 changes: 13 additions & 12 deletions app/application.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
jQuery(function($){

window.TaskController = Spine.Controller.create({
window.Tasks = Spine.Controller.create({
tag: "li",

scoped: ["render", "remove"],

events: {
"change input[type=checkbox]": "toggle",
"click .destroy": "destroy",
Expand All @@ -17,7 +19,6 @@ jQuery(function($){
},

init: function(){
this.proxyAll("render", "remove");
this.item.bind("update", this.render);
this.item.bind("destroy", this.remove);
},
Expand Down Expand Up @@ -47,7 +48,7 @@ jQuery(function($){
if (e.keyCode == 13) e.target.blur();
},

close: function(e){
close: function(){
this.wrapper.removeClass("editing");
this.item.updateAttributes({name: this.input.val()});
},
Expand All @@ -57,13 +58,14 @@ jQuery(function($){
}
});

window.AppController = Spine.Controller.create({
window.TaskApp = Spine.Controller.create({
el: $("#tasks"),

scoped: ["addOne", "addAll", "renderCount"],

events: {
"submit form": "create",
"click .clear": "clear",
"render .items": "renderCount"
"submit form": "create",
"click .clear": "clear"
},

elements: {
Expand All @@ -74,23 +76,22 @@ jQuery(function($){
},

init: function(){
this.proxyAll("addOne", "addAll", "renderCount");
Task.bind("create", this.addOne);
Task.bind("refresh", this.addAll);
Task.bind("refresh change", this.renderCount);
Task.fetch();
},

addOne: function(e, task) {
var view = TaskController.inst({item: task || e});
addOne: function(task) {
var view = Tasks.inst({item: task});
this.items.append(view.render().el);
},

addAll: function() {
Task.each(this.addOne);
},

create: function(e){
create: function(){
Task.create({name: this.input.val()});
this.input.val("");
return false;
Expand All @@ -109,5 +110,5 @@ jQuery(function($){
}
});

window.App = AppController.inst();
window.App = TaskApp.inst();
});
78 changes: 57 additions & 21 deletions lib/spine.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
var dupHash = function(hash){
var result = {};
for(var name in hash)
result[name] = hash[name]
result[name] = hash[name];
return result;
};

Expand All @@ -28,16 +28,15 @@
var calls = this._callbacks || (this._callbacks = {});

for (var i=0; i < evs.length; i++)
(this._callbacks[evs[i]] || (this._callbacks[evs[i]] = [])).push(callback)
(this._callbacks[evs[i]] || (this._callbacks[evs[i]] = [])).push(callback);

return this;
},

trigger: function() {
var args = makeArray(arguments);
var ev = args.shift();
args.splice(0, 0, {type: ev, target: this});


var list, calls, i, l;
if (!(calls = this._callbacks)) return this;
if (!(list = this._callbacks[ev])) return this;
Expand All @@ -46,6 +45,26 @@
if (list[i].apply(this, args) === false)
return false;
return this;
},

unbind: function(ev, callback){
if ( !ev ) {
this._callbacks = {};
return this;
}

var list, calls, i, l;
if (!(calls = this._callbacks)) return this;
if (!(list = this._callbacks[ev])) return this;

for (i = 0, l = list.length; i < l; i++) {
if (callback === list[i]) {
list.splice(i, 1);
break;
}
}

return this;
}
};

Expand All @@ -57,7 +76,7 @@
log: function(){
if ( !this.trace ) return;
if (typeof console == "undefined") return;
var args = $.makeArray(arguments);
var args = makeArray(arguments);
if (this.logPrefix) args.unshift(this.logPrefix);
console.log.apply(console, args);
return this;
Expand Down Expand Up @@ -88,7 +107,7 @@
var object = Object.create(this);
object.parent = this;
object.prototype = object.fn = Object.create(this.prototype);

if (include) object.include(include);
if (extend) object.extend(extend);

Expand Down Expand Up @@ -170,9 +189,15 @@
this.records = {};
this.attributes = [];

this.bind("create update destroy", function(e, record){
this.trigger("change", e.type, record);
});
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);
}));
},

find: function(id){
Expand All @@ -191,12 +216,13 @@

refresh: function(values){
this.records = {};

for (var i=0, il = values.length; i < il; i++) {
var record = this.inst(values[i]);
record.newRecord = false;
this.records[record.id] = record;
}

this.trigger("refresh");
},

Expand Down Expand Up @@ -277,13 +303,17 @@
fetch: function(callback){
callback ? this.bind("fetch", callback) : this.trigger("fetch");
},

toJSON: function(){
return this.recordsValues();
},

// Private

recordsValues: function(){
var result = []
var result = [];
for (var key in this.records)
result.push(this.records[key])
result.push(this.records[key]);
return result;
},

Expand Down Expand Up @@ -356,7 +386,9 @@
},

dup: function(){
return Object.create(this);
var result = this.parent.inst(this.attributes());
result.newRecord = this.newRecord;
return result;
},

reload: function(){
Expand All @@ -375,22 +407,18 @@
this.trigger("update");
},

generateID: function(){
return Spine.guid();
},

create: function(){
this.trigger("beforeCreate");
if ( !this.id ) this.id = this.generateID();
if ( !this.id ) this.id = Spine.guid();
this.newRecord = false;
this.parent.records[this.id] = this.dup();
this.trigger("create");
},

bind: function(events, callback){
this.parent.bind(events, this.proxy(function(e, record){
this.parent.bind(events, this.proxy(function(record){
if ( record && this.eql(record) )
callback.apply(this, arguments)
callback.apply(this, arguments);
}));
},

Expand Down Expand Up @@ -458,5 +486,13 @@
}
});

Controller.include(Events);
Controller.include(Events);
Controller.include(Log);

Spine.App = Spine.Controller.create({
create: function(properties){
this.parent.include(properties);
}
}).inst();
Spine.Controller.fn.App = Spine.App;
})();
2 changes: 1 addition & 1 deletion lib/spine.model.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Spine.Model.Local = {
},

saveLocal: function(){
var result = JSON.stringify(this.recordsValues());
var result = JSON.stringify(this);
localStorage[this.name] = result;
},

Expand Down

0 comments on commit 9d43499

Please sign in to comment.