Answer the question
In order to leave comments, you need to log in
What is the correct way to work with backbone.localStorage?
I can't figure out how to store data in Backbone via backbone.localStorage.
I create a collection, I add to it.
App.Collections.Elements= Backbone.Collection.extend({
model: App.Models.Element,
localStorage: new Backbone.LocalStorage("SomeCollection")
});
var elements = new App.Collections.Elements(elementsArray);
this.model.set({
surname: this.surname.val(),
name: this.name.val(),
middlename: this.middlename.val(),
tel: this.tel.val(),
email: this.email.val()
});
elements.add(this.model);
Answer the question
In order to leave comments, you need to log in
because the set method only sets the parameters of the model, and the save method is used to save the model
Thank you. But I'm probably doing something wrong again. I would not want to be impudent and get with questions, but the second day I just can’t figure out how to add the created models to the collection in localStorage. I understand the examples of using backbone.localStorage, but it doesn’t work.
1) I create an instance of the model
App.Models.Element= Backbone.Model.extend({
defaults: {
........
}
});
App.Collections.Elements= Backbone.Collection.extend({
model: App.Models.Element,
localStorage: new Backbone.LocalStorage("SomeCollection")
});
var elements= new App.Collections.Elements(elementsArray);
App.Views.Elements= Backbone.View.extend({
template: tpl('elementsTpl'),
initialize: function() {
this.listenTo(elements, 'add', this.render);
},
render: function() {
this.$el.html( this.template({ elements: elements.toJSON() }) );
return this;
}
});
<script id="elementsTpl" type="text/template">
<ul class="elements">
<% _.each(elements,function(element) { %>
<li class="element">
<div class="element-name"><%= element.name %></div>
<div class="element-date"><%= element.date%></div>
</li>
<% }); %>
</ul>
</script>
var elementsView = new App.Views.Elements;
$('#elements-container').html(elementsView .render().el);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question