Reputation: 4944
Collection definition:
define(function(require) {
var Backbone = require('backbone'),
m = require('../models/m');
require('backbone.localstorage');
var c = Backbone.Collection.extend({
model: m,
localStorage: new Backbone.LocalStorage('queue')
});
return new c();
});
In View:
define(function(require) {
var $ = require('jquery'),
_ = require('underscore'),
Backbone = require('backbone'),
c = require('../collections/c');
var v = Backbone.View.extend({
...
events: {
'click div': 'updateQueue'
},
updateQueue: function($_event) {
c.add(this.model);
}
});
return v;
});
The c.add(this.model);
line doesn't work at all, and the Collection never gets saved into localStorage, losing all Models on each page reload. Any idea where I did wrong? I'm using the newest Backbone.localStorage version for Github.
EDIT:
Changed to c.create()
instead and it's still not saving anything to localStorage. Any ideas?
Upvotes: 0
Views: 732
Reputation: 4944
Found the bug:
updateQueue: function($_event) {
c.create(this.model); // wrong
c.create(this.model.toJSON()); // correct!
}
Upvotes: 1
Reputation: 19821
If you added the model to collection, it doen't mean it's got saved. You have to call save method, by yourself.
Good recommendation to use create
instead.
Upvotes: 0
Reputation: 1110
Try calling the create() function c.create(this.model);
, this automatically saves your model.
Or you can save the model before you add it to collection
Upvotes: 1