Skip to content

Commit

Permalink
added: store method. additional documentation + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emaphp committed Mar 23, 2015
1 parent 35dc061 commit 1bf9f84
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Backbone Models meet Promises
###About

<br/>
Backbone.Async introduces a Model and a Collection class that wrap their sync methods into a Promise.
Backbone.Async introduces a Model and a Collection class that wrap their syncronization methods using [Promises](http://www.html5rocks.com/en/tutorials/es6/promises/ "").

<br/>
###Acknowledgement
Expand Down Expand Up @@ -40,7 +40,7 @@ Browsers not supporting Promises should use a polyfill. You can find one [here](
**Backbone.Async.Collection**
```javascript
var Contacts = Backbone.Async.Collection.extend({
url: 'http://example.com/contacts'
url: '/contacts'
});

var contacts = new Contacts();
Expand Down Expand Up @@ -218,6 +218,21 @@ var NotesStore = Backbone.Async.Storage.extend({
var storage = new NotesStore();
```

<br/>
**Storing models**
```javascript
var note = new Note({message: 'Hello'});

note.save()
.then(function(data) {
storage.store(model);
console.log('Model saved');
})
.catch(function(data) {
console.log('Failed to save model:', data.response.statusText);
});
```

<br/>
**Get model**
```javascript
Expand Down
12 changes: 10 additions & 2 deletions dist/backbone.async.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
}(this, function(global, Backbone, _) {
var overrideCallback = function(callback, resolver, cb_options) {
return function(model, response, options) {
if (callback)
callback.apply(model, arguments);
if (callback) callback.apply(model, arguments);

var data = {
response: response,
Expand Down Expand Up @@ -192,6 +191,15 @@
reject(data);
});
});
},

store: function(model) {
if (!this.Collection) throw new Error('No Collection class defined');
if (!this.collection) this.collection = new this.Collection();
this.collection.push(model);
this.listenTo(model, 'after:destroy', function(data, success) {
if (success) this.collection.remove(data.model, {silent: true});
});
}
});

Expand Down
2 changes: 1 addition & 1 deletion dist/backbone.async.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions test/specs/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,4 +830,87 @@ describe("ASync.Storage tests", function() {
server.respond();
})
});

describe('Save tests', function() {
it('must save value', function(done) {
server.respondWith(
'POST',
'/contacts',
[
200,
{"Content-Type": "application/json"},
JSON.stringify({id: 1, name: 'Emmanuel', surname: 'Antico'})
]
);

var storage = new FIXTURES.ContactsStorage();
var model = new FIXTURES.Contact();

model.set({name: 'Emmanuel', surname: 'Antico'});

model.save()
.then(function(data) {
storage.store(model);
expect(storage.collection).to.be.a('object');
expect(storage.collection.length).to.equal(1);
var contact = storage.collection.get(1);
expect(contact.attributes).to.deep.equal(model.attributes);
done();
})
.catch(function(err) {done(err)});

server.respond();
});
});

describe('Update tests', function() {
it('must update attributes', function(done) {
var value = JSON.stringify({id: 1, name: 'Emmanuel', surname: 'Antico'});

server.respondWith(
'GET',
'/contacts/1',
[
200,
{"Content-Type": "application/json"},
value
]
);

server.respondWith(
'PUT',
'/contacts/1',
[
200,
{"Content-Type": "application/json"},
value.replace('Emmanuel', 'emaphp')
]
);

var storage = new FIXTURES.ContactsStorage();

storage.get(1)
.then(function(data) {
var model = data.model;
model.set('name', 'emaphp');
expect(storage.collection.get(1).get('name')).to.equal('emaphp');

model.save()
.then(function(data) {
expect(storage.collection.get(1).get('name')).to.equal('emaphp');
done();
})
.catch(function(err) {
done(err);
});

server.respond();
})
.catch(function(err) {
done(err);
});

server.respond();
});
});
});

0 comments on commit 1bf9f84

Please sign in to comment.