Skip to content

Commit

Permalink
added: delete test for stored models. additional documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
emaphp committed Mar 22, 2015
1 parent 8e870bd commit 35dc061
Show file tree
Hide file tree
Showing 4 changed files with 651 additions and 21 deletions.
79 changes: 75 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contacts.fetch()
if (_.isError(data))
console.error(data)
else
console.log('Failed to fetch collection');
console.log('Failed to fetch collection:', data.response.statusText);
});
```

Expand Down Expand Up @@ -83,7 +83,7 @@ contact.fetch()
if (_.isError(data))
console.error(data)
else
console.log('Failed to fetch contact');
console.log('Failed to fetch contact:', data.response.statusText);
});
```

Expand All @@ -105,7 +105,7 @@ Backbone.Async adds additional events when synchronizing a Model or Collection.

```javascript
var Contacts = Backbone.Async.Collection.extend({
url: 'http://example.com/contacts'
url: '/contacts'
});

var contacts = new Contacts();
Expand Down Expand Up @@ -147,7 +147,7 @@ The *after:fetch* event handler receives the exact same object provided to the f

```javascript
var Contact = Backbone.Async.Contact.extend({
urlRoot: 'http://example.com/contacts'
urlRoot: '/contacts'
});

var contact = new Contact({id: 1});
Expand Down Expand Up @@ -194,6 +194,77 @@ The *before:fetch* and *before:destroy* event handlers will receive an object co

The *before:save* event handler argument also includes an additional property named *attrs* with the attributes being saved. All *after:[event]* handlers receive the exact same object provided to the fulfilled and rejection callbacks plus a *success* argument.

<br/>
###Storage

<br/>
The Storage class provides an additional wrapper for Model and Collection classes.

```javascript
var Note = Backbone.Async.Model.extend({
//...
});

var Notes = Backbone.Async.Collection.extend({
//...
});

//extend the Storage class
var NotesStore = Backbone.Async.Storage.extend({
Model: Note,
Collection: Notes
});

var storage = new NotesStore();
```

<br/>
**Get model**
```javascript
//obtain model with ID 1
storage.get(1, {option: 'value'})
.then(function(data) {
var note = storage.collection.get(1); //returns model

//the 'get' method can be called more than once
//the returned model will be stored internally
//the 'response' property will not be set if the object was alreasy loaded
if (data.response) {
console.log('A request has been made');
}
})
.catch(function(data_or_err) {
if (_.isError(data_or_err))
console.error(err);
else
console.log(data.statusText);
});
```

<br/>
**Get collection**

```javascript
//fetch all models
storage.fetch({option: 'value'})
.then(function(data) {
storage.isLoaded === true; //returns true

//same response rule applies to 'fetch'
if (data.response) {
console.log('A request has been made');
}
})
.catch(function(data_or_err) {
if (_.isError(data_or_err))
console.error(err);
else
console.log(data.statusText);
});
```

The Storage class also triggers before/after events when *get* and *fetch* are called.

<br/>
###License

Expand Down
31 changes: 15 additions & 16 deletions dist/backbone.async.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@
return methods.reduce(wrapMethod(Base.prototype), {});
};

//create namespace
var Async = Backbone.Async = Backbone.Async || {};
Async.VERSION = '1.1.0';

//extend Model and Collection prototypes
Async.Model = Backbone.Model.extend(
buildPrototype(Backbone.Model, ['fetch', 'save', 'destroy'])
);
Expand All @@ -111,8 +113,7 @@
if (options) {
if (options.Collection) this.Collection = options.Collection;

if (options.Model)
this.Model = options.Model;
if (options.Model) this.Model = options.Model;
else if (options.Collection && options.Collection.model)
this.Model = options.Collection.model;
}
Expand All @@ -132,8 +133,7 @@
});
}

if (!this.Collection)
throw new Error('No Collection class defined');
if (!this.Collection) throw new Error('No Collection class defined');

options = options || {};
var self = this;
Expand All @@ -158,20 +158,16 @@
get: function(id, options) {
if (this.collection) {
var value = this.collection.get(id);
var data = {
model: value,
options: options
};

if (!value) return Promise.reject(data);
return Promise.resolve(data);
if (value) {
return Promise.resolve({
model: value,
options: options
});
}
}

if (!this.Model)
throw new Error('No Model class defined');

if (!this.Collection)
throw new Error('No Collection class defined');
if (!this.Model) throw new Error('No Model class defined');
if (!this.Collection) throw new Error('No Collection class defined');

options = options || {};
var self = this;
Expand All @@ -186,6 +182,9 @@
if (mustTrigger) self.trigger('after:get', data, true);
if (!self.collection) self.collection = new self.Collection();
self.collection.push(model);
self.listenTo(model, 'after:destroy', function(data, success) {
if (success) self.collection.remove(data.model, {silent: true});
});
resolve(data);
})
.catch(function(data) {
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.

Loading

0 comments on commit 35dc061

Please sign in to comment.