Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Always pass the same response from read/update/create #34

Closed
wants to merge 1 commit into from

2 participants

@kjaebker

Create and Update were returning the models toJson function. This is not always the same as what is stored in local storage (which is written using JSON.stringify(model)). This results in the parse method receiving different values depending on what method is called.

@kjaebker kjaebker Create & Update should return same result as read
Create and Update were returning the models toJson function. This is
not always the same as what is stored in local storage. This results in
the parse method receiving different values depending on what method is
called.
07e89f4
@jeromegn
Owner

Why not return model? (real question, I've been out of the game a bit, not sure what's the advantage of fetching the model again)

@kjaebker

From what I understand update/create/read sync methods all get their response passed to the parse method. This method expects (based on the documentation) the raw response from the server. So the change I put in here standardizes those responses.

The reason I found this is because I have a collection as an attribute for a model. When this model gets written to local storage the json stringfy converts the collection into an array. When I retrieve that model again I need to parse the result and re-create that collection.

This could be done by calling stringify on the model like when it saves, but I figured just calling find to get would suffice.

@jeromegn jeromegn closed this pull request from a commit
@jeromegn Mega test refactor, now using Mocha. Some tests were utterly useless …
…as they tested Backbone functionalities. Fixes #34
44815bd
@jeromegn jeromegn closed this in 44815bd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 25, 2012
  1. @kjaebker

    Create & Update should return same result as read

    kjaebker authored
    Create and Update were returning the models toJson function. This is
    not always the same as what is stored in local storage. This results in
    the parse method receiving different values depending on what method is
    called.
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 3 deletions.
  1. +1 −1  backbone.localStorage-min.js
  2. +2 −2 backbone.localStorage.js
View
2  backbone.localStorage-min.js
@@ -1,4 +1,4 @@
/**
* Backbone localStorage Adapter
* https://github.com/jeromegn/Backbone.localStorage
- */(function(){function n(){return((1+Math.random())*65536|0).toString(16).substring(1)}function r(){return n()+n()+"-"+n()+"-"+n()+"-"+n()+"-"+n()+n()+n()}var e=this._,t=this.Backbone;t.LocalStorage=window.Store=function(e){this.name=e;var t=this.localStorage().getItem(this.name);this.records=t&&t.split(",")||[]},e.extend(t.LocalStorage.prototype,{save:function(){this.localStorage().setItem(this.name,this.records.join(","))},create:function(e){return e.id||(e.id=r(),e.set(e.idAttribute,e.id)),this.localStorage().setItem(this.name+"-"+e.id,JSON.stringify(e)),this.records.push(e.id.toString()),this.save(),e.toJSON()},update:function(t){return this.localStorage().setItem(this.name+"-"+t.id,JSON.stringify(t)),e.include(this.records,t.id.toString())||this.records.push(t.id.toString()),this.save(),t.toJSON()},find:function(e){return JSON.parse(this.localStorage().getItem(this.name+"-"+e.id))},findAll:function(){return e(this.records).chain().map(function(e){return JSON.parse(this.localStorage().getItem(this.name+"-"+e))},this).compact().value()},destroy:function(t){return this.localStorage().removeItem(this.name+"-"+t.id),this.records=e.reject(this.records,function(e){return e==t.id.toString()}),this.save(),t},localStorage:function(){return localStorage}}),t.LocalStorage.sync=window.Store.sync=t.localSync=function(e,t,n,r){var i=t.localStorage||t.collection.localStorage;typeof n=="function"&&(n={success:n,error:r});var s;switch(e){case"read":s=t.id!=undefined?i.find(t):i.findAll();break;case"create":s=i.create(t);break;case"update":s=i.update(t);break;case"delete":s=i.destroy(t)}s?n.success(s):n.error("Record not found")},t.ajaxSync=t.sync,t.getSyncMethod=function(e){return e.localStorage||e.collection&&e.collection.localStorage?t.localSync:t.ajaxSync},t.sync=function(e,n,r,i){return t.getSyncMethod(n).apply(this,[e,n,r,i])}})();
+ */(function(){function n(){return((1+Math.random())*65536|0).toString(16).substring(1)}function r(){return n()+n()+"-"+n()+"-"+n()+"-"+n()+"-"+n()+n()+n()}var e=this._,t=this.Backbone;t.LocalStorage=window.Store=function(e){this.name=e;var t=this.localStorage().getItem(this.name);this.records=t&&t.split(",")||[]},e.extend(t.LocalStorage.prototype,{save:function(){this.localStorage().setItem(this.name,this.records.join(","))},create:function(e){return e.id||(e.id=r(),e.set(e.idAttribute,e.id)),this.localStorage().setItem(this.name+"-"+e.id,JSON.stringify(e)),this.records.push(e.id.toString()),this.save(),this.find(e)},update:function(t){return this.localStorage().setItem(this.name+"-"+t.id,JSON.stringify(t)),e.include(this.records,t.id.toString())||this.records.push(t.id.toString()),this.save(),this.find(t)},find:function(e){return JSON.parse(this.localStorage().getItem(this.name+"-"+e.id))},findAll:function(){return e(this.records).chain().map(function(e){return JSON.parse(this.localStorage().getItem(this.name+"-"+e))},this).compact().value()},destroy:function(t){return this.localStorage().removeItem(this.name+"-"+t.id),this.records=e.reject(this.records,function(e){return e==t.id.toString()}),this.save(),t},localStorage:function(){return localStorage}}),t.LocalStorage.sync=window.Store.sync=t.localSync=function(e,t,n,r){var i=t.localStorage||t.collection.localStorage;typeof n=="function"&&(n={success:n,error:r});var s;switch(e){case"read":s=t.id!=undefined?i.find(t):i.findAll();break;case"create":s=i.create(t);break;case"update":s=i.update(t);break;case"delete":s=i.destroy(t)}s?n.success(s):n.error("Record not found")},t.ajaxSync=t.sync,t.getSyncMethod=function(e){return e.localStorage||e.collection&&e.collection.localStorage?t.localSync:t.ajaxSync},t.sync=function(e,n,r,i){return t.getSyncMethod(n).apply(this,[e,n,r,i])}})();
View
4 backbone.localStorage.js
@@ -49,14 +49,14 @@ _.extend(Backbone.LocalStorage.prototype, {
this.localStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
this.records.push(model.id.toString());
this.save();
- return model.toJSON();
+ return this.find(model);
},
// Update a model by replacing its copy in `this.data`.
update: function(model) {
this.localStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
if (!_.include(this.records, model.id.toString())) this.records.push(model.id.toString()); this.save();
- return model.toJSON();
+ return this.find(model);
},
// Retrieve a model from `this.data` by id.
Something went wrong with that request. Please try again.