Skip to content

Instantly share code, notes, and snippets.

@EyePulp
Created June 19, 2013 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EyePulp/5815203 to your computer and use it in GitHub Desktop.
Save EyePulp/5815203 to your computer and use it in GitHub Desktop.
Basic attempt @ using knockback to read from a tastypie resource and add to a collection. Wondering how to properly and most succinctly create a new Payment instance on the client side and have it show up in its finished, saved incarnation via the observableCollection. Wondering if there's any two-way binding between the collection and the colle…
var Payment = kb.Model.extend({
urlRoot: '/api/v1/payment_backbone/'
});
var PaymentCollection = kb.Collection.extend({
model: Payment
// Constructor method
, initialize: function(data, options) {
this.limit = options.limit || 20;
this.offset = options.offset || 0;
this.total = 0; // set after fetch
this.hasMore = false; // set after fetch
}
, url: function() {return '/api/v1/payment_backbone/?' + this.params();}
// return the parameters for the url
, params: function() {
var p = {limit: this.limit, offset: this.offset};
return $.param(p);
}
// parse the results from the fetch() call.
, parse: function(resp) {
this.total = resp.meta.total_count;
this.offset = resp.meta.offset + this.limit;
this.hasMore = this.total > this.models.length;
return resp.objects;
}
});
var vm = {
payments : kb.collectionObservable(new PaymentCollection(null, {}))
};
vm.payments.subscribe(function(val){
console.log('payments',val[val.length-1]);
});
vm.payments.collection().fetch();
setTimeout(function(){
var m = new Payment({amount: '66.88'});
m.save(null,{success:function(data){
vm.payments.collection().add(m);
}});
},5000);
ko.applyBindings(vm);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment