From bc1bc0fbbdfd9b5d93a724279a08ed6e70afc842 Mon Sep 17 00:00:00 2001 From: Makis Tracend Date: Thu, 22 Nov 2012 04:21:29 -0800 Subject: [PATCH] #10 Adding custom model/collection got MongoDB parsing --- build/backbone.app-min.js | 2 +- build/backbone.app.js | 33 ++++++++++++++++++++++++++++++++- lib/app.collection.js | 15 +++++++++++++++ lib/app.model.js | 16 ++++++++++++++++ readme.md | 2 +- 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/build/backbone.app-min.js b/build/backbone.app-min.js index 51cbfce..6ee2e8f 100644 --- a/build/backbone.app-min.js +++ b/build/backbone.app-min.js @@ -1 +1 @@ -(function(){APP=function(){var t=!1,e=window.location.pathname.split("/");for(var i in e)if(e[i]!==""&&(t=e[i].charAt(0).toUpperCase()+e[i].slice(1),typeof APP.Routers[t]=="function"))break;var n=t&&APP.Routers[t]?new APP.Routers[t]:new APP.Routers.Default;return n},APP.Models={},APP.Routers={},APP.Collections={},APP.Views={},APP.Templates={}})(this._,this.Backbone),function(t){t.templateSettings={interpolate:/\{\{(.+?)\}\}/g}}(this._,this.Backbone,this.jQuery),function(t,e){Model=e.Model.extend({cache:function(){},isOnline:function(){return t.isUndefined(app)?!0:app.state.online},sync:function(i,n,a){var r={create:"POST",update:"PUT","delete":"DELETE",read:"GET"},s=r[i];a||(a={});var o={type:s,dataType:"json",data:{}};return a.url||(o.url=this.getValue(n,"url")||urlError()),a.data||!n||i!="create"&&i!="update"||(o.contentType="application/json",o.data=JSON.stringify(n.toJSON())),o.type==="GET"||e.emulateJSON||(o.processData=!1),$.ajax(t.extend(o,a))},getValue:function(e,i){return e&&e[i]?t.isFunction(e[i])?e[i]():e[i]:null}})}(this._,this.Backbone),function(t,e){Collection=e.Collection.extend({initialize:function(e,i){this.options=i||{},t.isNull(e)&&this.fetch()},attributes:{},add:function(i,n){var a={};return i=t.isArray(i)?i.slice():[i],t.each(i,function(e){t.isUndefined(e.id)?a["add_model_"+Math.random()]=e:t.isUndefined(this.get(e.id))?a[e.id]=e:this.set(e)},this),a=t.toArray(a),e.Collection.prototype.add.call(this,a,n)},set:function(e){var i=this.get(e.id),n=t.extend(i,e);this.remove(i),this.add(n)},update:function(){},setAttr:function(t){for(var e in t)this.attributes[e]=t[e]},getAttr:function(t){return this.attributes[t]},isOnline:function(){return t.isUndefined(app)?!0:app.state.online}})}(this._,this.Backbone),function(t,e,i){View=e.View.extend({events:{"click a[rel='external']":"clickExternal"},initialize:function(){t.bindAll(this,"render","clickExternal"),this.data=this.model||this.collection||null,this.options.type||(this.options.type="default"),this.options.attr?i(this.el).attr("data-view",this.options.attr):i(this.el).removeAttr("data-view");var e=this.options.html||null,n={};this.options.url&&(n.url=this.options.url),this.template=new Template(e,n),this.template.bind("loaded",this.render),t.isNull(this.data)||(this.data.bind("change",this.render),this.data.bind("reset",this.render),this.data.bind("add",this.render),this.data.bind("remove",this.render)),this.render()},render:function(){var e=this.options.type,n=this.template.get(e),a=t.isNull(this.data)?{}:this.data.toJSON();if(!t.isUndefined(n)){var r=n(a);this.options.append?i(this.el).append(r):i(this.el).html(r)}},listen:function(t,e,i){var n=typeof e=="string"?[e]:e;for(var a in n)t.bind(n[a],i)},clickExternal:function(e){e.preventDefault();var i=this.findLink(e.target);return t.isUndefined(pageTracker)||(i=pageTracker._getLinkerUrl(i)),window.open(i,"_blank"),!1},findLink:function(t){return t.tagName!="A"?i(t).closest("a").attr("href"):i(t).attr("href")}})}(this._,this.Backbone,this.jQuery),function(t,e,i){Template=e.Model.extend({initialize:function(e,i){t.bindAll(this,"fetch","parse"),i||(i={}),t.isEmpty(e)||(this.set("default",this.compile(e)),this.trigger("loaded")),i.url&&(this.url=i.url,this.fetch())},fetch:function(){i.get(this.url,this.parse)},parse:function(t){var e=this,n=i(t).filter("script");n.length?n.each(function(){var t=i(this);t.attr("type").indexOf("template")>=0&&e.set(t.attr("id"),e.compile(t.html()))}):this.set("default",e.compile(t)),this.trigger("loaded")}})}(this._,this.Backbone,this.jQuery),function(t,e){Router=e.Router.extend({initialize:function(){},state:{fullscreen:!1,online:navigator.onLine,browser:function(){return $.browser.safari&&/chrome/.test(navigator.userAgent.toLowerCase())?"chrome":/(iPhone|iPod).*OS 5.*AppleWebKit.*Mobile.*Safari/.test(navigator.userAgent)?"ios":"other"}},_trackPageview:function(){var i=e.history.getFragment();t.isUndefined(_gaq)||_gaq.push(["_trackPageview","/#"+i])}})}(this._,this.Backbone) \ No newline at end of file +(function(){APP=function(){var t=!1,e=window.location.pathname.split("/");for(var i in e)if(e[i]!==""&&(t=e[i].charAt(0).toUpperCase()+e[i].slice(1),typeof APP.Routers[t]=="function"))break;var n=t&&APP.Routers[t]?new APP.Routers[t]:new APP.Routers.Default;return n},APP.Models={},APP.Routers={},APP.Collections={},APP.Views={},APP.Templates={}})(this._,this.Backbone),function(t){t.templateSettings={interpolate:/\{\{(.+?)\}\}/g}}(this._,this.Backbone,this.jQuery),function(t,e){Model=e.Model.extend({cache:function(){},isOnline:function(){return t.isUndefined(app)?!0:app.state.online},sync:function(i,n,a){var r={create:"POST",update:"PUT","delete":"DELETE",read:"GET"},s=r[i];a||(a={});var o={type:s,dataType:"json",data:{}};return a.url||(o.url=this.getValue(n,"url")||urlError()),a.data||!n||i!="create"&&i!="update"||(o.contentType="application/json",o.data=JSON.stringify(n.toJSON())),o.type==="GET"||e.emulateJSON||(o.processData=!1),$.ajax(t.extend(o,a))},getValue:function(e,i){return e&&e[i]?t.isFunction(e[i])?e[i]():e[i]:null}}),MongoModel=Model.extend({parse:function(t){return t._id&&(t.id=t._id,delete t._id),t}})}(this._,this.Backbone),function(t,e){Collection=e.Collection.extend({initialize:function(e,i){this.options=i||{},t.isNull(e)&&this.fetch()},attributes:{},add:function(i,n){var a={};return i=t.isArray(i)?i.slice():[i],t.each(i,function(e){t.isUndefined(e.id)?a["add_model_"+Math.random()]=e:t.isUndefined(this.get(e.id))?a[e.id]=e:this.set(e)},this),a=t.toArray(a),e.Collection.prototype.add.call(this,a,n)},set:function(e){var i=this.get(e.id),n=t.extend(i,e);this.remove(i),this.add(n)},update:function(){},setAttr:function(t){for(var e in t)this.attributes[e]=t[e]},getAttr:function(t){return this.attributes[t]},isOnline:function(){return t.isUndefined(app)?!0:app.state.online}}),MongoCollection=Collection.extend({parse:function(t){for(var e in t)t[e].id=t[e]._id,delete t[e]._id;return t}})}(this._,this.Backbone),function(t,e,i){View=e.View.extend({events:{"click a[rel='external']":"clickExternal"},initialize:function(){t.bindAll(this,"render","clickExternal"),this.data=this.model||this.collection||null,this.options.type||(this.options.type="default"),this.options.attr?i(this.el).attr("data-view",this.options.attr):i(this.el).removeAttr("data-view");var e=this.options.html||null,n={};this.options.url&&(n.url=this.options.url),this.template=new Template(e,n),this.template.bind("loaded",this.render),t.isNull(this.data)||(this.data.bind("change",this.render),this.data.bind("reset",this.render),this.data.bind("add",this.render),this.data.bind("remove",this.render)),this.render()},render:function(){var e=this.options.type,n=this.template.get(e),a=t.isNull(this.data)?{}:this.data.toJSON();if(!t.isUndefined(n)){var r=n(a);this.options.append?i(this.el).append(r):i(this.el).html(r)}},listen:function(t,e,i){var n=typeof e=="string"?[e]:e;for(var a in n)t.bind(n[a],i)},clickExternal:function(e){e.preventDefault();var i=this.findLink(e.target);return t.isUndefined(pageTracker)||(i=pageTracker._getLinkerUrl(i)),window.open(i,"_blank"),!1},findLink:function(t){return t.tagName!="A"?i(t).closest("a").attr("href"):i(t).attr("href")}})}(this._,this.Backbone,this.jQuery),function(t,e,i){Template=e.Model.extend({initialize:function(e,i){t.bindAll(this,"fetch","parse"),i||(i={}),t.isEmpty(e)||(this.set("default",this.compile(e)),this.trigger("loaded")),i.url&&(this.url=i.url,this.fetch())},fetch:function(){i.get(this.url,this.parse)},parse:function(t){var e=this,n=i(t).filter("script");n.length?n.each(function(){var t=i(this);t.attr("type").indexOf("template")>=0&&e.set(t.attr("id"),e.compile(t.html()))}):this.set("default",e.compile(t)),this.trigger("loaded")}})}(this._,this.Backbone,this.jQuery),function(t,e){Router=e.Router.extend({initialize:function(){},state:{fullscreen:!1,online:navigator.onLine,browser:function(){return $.browser.safari&&/chrome/.test(navigator.userAgent.toLowerCase())?"chrome":/(iPhone|iPod).*OS 5.*AppleWebKit.*Mobile.*Safari/.test(navigator.userAgent)?"ios":"other"}},_trackPageview:function(){var i=e.history.getFragment();t.isUndefined(_gaq)||_gaq.push(["_trackPageview","/#"+i])}})}(this._,this.Backbone) \ No newline at end of file diff --git a/build/backbone.app.js b/build/backbone.app.js index 7ec5b81..db2420b 100644 --- a/build/backbone.app.js +++ b/build/backbone.app.js @@ -1,6 +1,6 @@ /**@license * backbone.app <> - * Version: 0.6.0 (Mon, 12 Nov 2012 02:03:37 GMT) + * Version: 0.6.0 (Thu, 22 Nov 2012 12:07:04 GMT) * License: */ (function(_, Backbone) { @@ -85,6 +85,22 @@ } }); + + // *** Extensions *** + + MongoModel = Model.extend({ + + parse: function( data ){ + //console.log(data); + // "normalize" result with proper ids + if(data._id){ + data.id = data._id; + delete data._id; + } + return data; + } + }); + })(this._, this.Backbone); (function(_, Backbone) { @@ -158,6 +174,21 @@ } }); + + // *** Extensions *** + + MongoCollection = Collection.extend({ + + parse: function( data ){ + //console.log(data); + // "normalize" result with proper ids + for(var i in data){ + data[i].id = data[i]._id; + delete data[i]._id; + } + return data; + } + }); })(this._, this.Backbone); (function(_, Backbone, $) { diff --git a/lib/app.collection.js b/lib/app.collection.js index bf84aa6..bd8d39f 100644 --- a/lib/app.collection.js +++ b/lib/app.collection.js @@ -70,4 +70,19 @@ } }); + + // *** Extensions *** + + MongoCollection = Collection.extend({ + + parse: function( data ){ + //console.log(data); + // "normalize" result with proper ids + for(var i in data){ + data[i].id = data[i]._id; + delete data[i]._id; + } + return data; + } + }); })(this._, this.Backbone); \ No newline at end of file diff --git a/lib/app.model.js b/lib/app.model.js index 597f044..f1509ac 100644 --- a/lib/app.model.js +++ b/lib/app.model.js @@ -41,4 +41,20 @@ } }); + + // *** Extensions *** + + MongoModel = Model.extend({ + + parse: function( data ){ + //console.log(data); + // "normalize" result with proper ids + if(data._id){ + data.id = data._id; + delete data._id; + } + return data; + } + }); + })(this._, this.Backbone); \ No newline at end of file diff --git a/readme.md b/readme.md index f7ce3e7..2cf15e7 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ An APP for your client app using simple conventions in Backbone.js ## Usage -Find the compilled library in the "build/" folder. Choose between the uncompressed and comment annotated **backbone.app.js** and the minified **backbone.app.min.js** +Find the compilled library in the "build/" folder. Choose between the uncompressed and comment annotated **backbone.app.js** and the minified **backbone.app-min.js** ## Build