Created
April 8, 2012 18:08
-
-
Save bingomanatee/2338856 to your computer and use it in GitHub Desktop.
date filtering in Backbone.js
This file contains hidden or 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
$(function () { | |
var pages = []; | |
var PageModel = Backbone.Model.extend({ | |
defaults:{ | |
title:"Untitled", | |
body:'New Article', | |
created:new Date(), // not precisely accurate but better than nothing | |
published:false, | |
by: 'Anonymous' | |
}, | |
initialize: function() { | |
console.log('this attributes: ', this.attributes); | |
if (_.isString(this.attributes.created)){ | |
try { | |
this.attributes.created = new Date(this.attributes.created); | |
} catch(e){ | |
this.attributes.created = new Date(); | |
} | |
} | |
}, | |
sync:function (method, model, options) { | |
switch (method) { | |
case 'create': | |
if (!model.get('id')) { | |
model.set('id', _.uniqueId('i')); | |
} | |
console.log('creating', model); | |
break; | |
case 'get': | |
return model.collection.get(options.id); | |
break; | |
case 'post': | |
console.log('posting', model); | |
break; | |
case 'delete': | |
model.collection.delete(model); | |
break; | |
case 'put': | |
console.log('putting', model); | |
break; | |
default: | |
console.log('unhandled: ', method, model); | |
} | |
} | |
} | |
); | |
BB2D.models.PageModel = PageModel; | |
var PageColl = Backbone.Collection.extend({ | |
model:BB2D.models.PageModel | |
}); | |
BB2D.coll.PageColl = PageColl; | |
BB2D.db = { | |
pages:new PageColl(pages) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment