Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
131 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,95 @@ | |||
(function(){ | |||
var Core = this.Core = {}; | |||
|
|||
Core.Model = Backbone.Model.extend({ | |||
urlRoot : '/data', | |||
defaults: { | |||
id: null, | |||
name: '' | |||
}, | |||
|
|||
clear: function() { | |||
this.destroy(); | |||
this.view.remove(); | |||
} | |||
}); | |||
|
|||
Core.Collection = Backbone.Collection.extend({ | |||
model: Core.Model, | |||
url: '/data', | |||
/* To prevent JSON array hijacking (http://haacked.com/archive/2009/06/25/json-hijacking.aspx) | |||
all collection data returned from the server should look like: {data : [...]} | |||
*/ | |||
parse : function(resp, xhr) { | |||
if (resp.data) { | |||
return resp.data; | |||
} | |||
return []; | |||
} | |||
}); | |||
|
|||
Core.Template = { | |||
cache: {}, //cached templates | |||
pending: {}, // queue of callbacks, waiting for template to load | |||
load: function(url, callback) { | |||
if (this.cache[url]) { //template loaded, call cb | |||
if (callback) callback(this.cache[url]); | |||
return; | |||
} | |||
|
|||
this.pending[url] = this.pending[url] || []; | |||
if (callback) this.pending[url].push(callback); //add callback to the queue | |||
|
|||
jQuery.ajax({ //load template | |||
url : url, | |||
dataType: 'text', | |||
complete: function(resp) { | |||
var cache = | |||
this.cache[url] = _.template(resp.responseText); // cache it | |||
|
|||
// proccess all pending callbacks | |||
_.each(this.pending[url], function(cb) { | |||
cb(cache); | |||
}); | |||
|
|||
delete this.pending[url]; | |||
}.bind(this), | |||
error: function() { | |||
throw new Error("Could not load " + url); | |||
} | |||
}); | |||
} | |||
}; | |||
|
|||
Core.View = Backbone.View.extend({ | |||
el: null, | |||
renderQueue: false, | |||
|
|||
initialize: function() { | |||
if (this.templateUrl) { | |||
Core.Template.load(this.templateUrl, function(data) { | |||
this.template = data; | |||
if (this.renderQueue !== false) { | |||
_.each(this.renderQueue, function(item) { | |||
this._render.apply(this, item); | |||
}.bind(this)); | |||
|
|||
this.renderQueue = false; | |||
} | |||
}.bind(this)); | |||
} | |||
}, | |||
|
|||
_render: function() {}, //Override this for render proccess | |||
|
|||
render: function() { | |||
if (!this.template) { | |||
this.renderQueue = this.renderQueue || []; | |||
this.renderQueue.push(arguments); | |||
return this; | |||
} | |||
return this._render.apply(this, arguments); | |||
} | |||
}); | |||
|
|||
})(); |
This file contains 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
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
<div class="todo <%= done ? 'done' : '' %>"> | |||
<div class="display"> | |||
<input class="check" type="checkbox" <%= done ? 'checked="checked"' : '' %> /> | |||
<div class="todo-text"></div> | |||
<span class="todo-destroy"></span> | |||
</div> | |||
<div class="edit"> | |||
<input class="todo-input" type="text" value="" /> | |||
</div> | |||
</div> |
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,14 @@ | |||
<% if (total) { %> | |||
<span class="todo-count"> | |||
<span class="number"><%= remaining %></span> | |||
<span class="word"><%= remaining == 1 ? 'item' : 'items' %></span> left. | |||
</span> | |||
<% } %> | |||
<% if (done) { %> | |||
<span class="todo-clear"> | |||
<a href="#"> | |||
Clear <span class="number-done"><%= done %></span> | |||
completed <span class="word-done"><%= done == 1 ? 'item' : 'items' %></span> | |||
</a> | |||
</span> | |||
<% } %> |