I have a collection in backbone like this:
app.ledgerList=Backbone.Collection.extend({
model: app.ledger,
localStorage:new Store('ledgers')
});
///initializing
app.ledgers=new app.ledgerList();
And in one of my views later in app, i have:
saveLedger: function(){
name=$("#ledgerName").val();
email=$("#ledgerEmail").val();
address=$("#ledgerAddress").val();
phone=$("#ledgerPhone").val();
console.log(app.ledgers.length);
app.ledgers.create({name:name,email:email,address:address,phone:phone});
}
This returns me Uncaught TypeError: Object [object Object] has no method 'apply'
on the line app.ledgers.create({name:name,email:email,address:address,phone:phone})
What may be the problem?
It looks like at the time you're calling create()
that app.ledgers
is no longer a Collection. You should console.log(app.ledgers)
right above the create() statement.
来源:https://stackoverflow.com/questions/14422433/unable-to-use-create-method-on-collections-in-backbonejs