Created
June 19, 2013 15:26
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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