集合中的主干模型数据未保存到本地存储
Backbone model data in collection not getting saved to localstorage
我制作了一个玩具应用程序,将比赛时间存储到通过HTML输入文本字段输入的localStorage中。 当我单击"添加种族"(基本保存)来存储新种族的信息时,首次创建集合时实例化的模型数据似乎被新的种族信息所取代。 这就像 collection.fetch() 调用正在销毁任何现有的集合数据。 我只想将比赛模型数据添加到现有的模型集合中。 我做错了什么?
应用.js
// js/app.js
var app = app || {};
var r_obj_arr=[new app.RaceModel({race_name:'Race1', race_date: new Date('November 25, 2013'), race_time: '1:00:01', race_distance: '10K'}), new app.RaceModel({race_name:'Race2', race_date: new Date('November 28, 2013'), race_time: '2:00:01', race_distance: '15K'})];
// Create our global collection of **Races**.
app.Races = new RaceList(r_obj_arr);
$(function() {
// Kick things off by creating the **App**.
new app.RaceView({model: r_obj_arr[0], collection: app.Races});
});
种族模型.js
// Race Model
// ----------
// Our basic **Race** model with default attributes
var app = app || {};
app.RaceModel = Backbone.Model.extend({
initialize: function(attributes) {
this.race_name = attributes.race_name;
this.race_date = attributes.race_date;
this.race_time = attributes.race_time;
this.race_distance = attributes.race_distance;
},
// Default attributes ensure that each todo created has `title` and `completed` keys.
defaults: {
race_name: 'default race name',
race_date: new Date("October 25, 2013"),
race_time: '1:06:25',
race_distance: '5K'
}
});
种族.js
var app = app || {};
// Race Collection
// ---------------
// The collection of Races is backed by *localStorage* instead of a remote
// server.
var RaceList = Backbone.Collection.extend({
initialize: function() {
},
// Reference to this collection's model.
model: app.RaceModel,
// Save all of the race items under the `"races-backbone"` namespace.
localStorage: new Backbone.LocalStorage('races-backbone'),
});
// Create our global collection of **Races**.
app.Races = new RaceList();
赛马观.js
var app = app || {};
app.RaceView = Backbone.View.extend({
// Instead of generating a new element, bind to the existing skeleton of
// the App already present in the HTML.
el: '#race-display',
model: app.RaceModel,
collection: app.Races,
// Our template for the line of statistics at the bottom of the app.
raceTemplate: _.template( $('#racetemplate').html() ),
// Delegated events for displaying new questions, and clearing existing ones
events: {
'click #add_id': 'addRace'
},
// The RaceView listens for changes to its model, re-rendering. Since there's
// a one-to-one correspondence between a **Question** and a **RaceView** in this
// app, we set a direct reference on the model for convenience.
initialize: function() {
// http://stackoverflow.com/questions/6079055/why-do-bindall-in-backbone-js-views
_.bindAll(this, 'render','addRace');
this.collection.bind('add', this.render);
this.collection.bind('reset', this.render);
this.render();
},
render: function(){
//_.extend is an underscore.js method to combine multiple JSON objects into one
this.$el.html(this.raceTemplate(_.extend({},this.model.toJSON(),{collection_obj: this.collection})));
return this;
},
addRace: function() {
var racename = this.$('#race-name-'+this.collection.length).val();
var racedate = new Date(this.$('#race-date-'+this.collection.length).val());
var racetime = this.$('#race-time-'+this.collection.length).val();
var racemodel = new app.RaceModel({race_name: racename, race_date: racedate, race_time: racetime});
this.collection.add(racemodel);
racemodel.save();
this.collection.fetch(); //refresh from localStorage
alert("Running data added to collection...");
}
});
索引.html
<body>
//: some other stuff
<div class="container">
<div class="row">
<h1>Running Times</h1>
<div id="race-display">
</div>
<script type="text/template" id="racetemplate">
<input type="button" id="add_id" value="Add Race" />
<% for(var i=0; i<this.collection.models.length; i++) { %>
<div class="col-md-3">
<input id="race-name-<%= i %>" placeholder="Name of race?" type="text" value="<%= this.collection.models[i].get('race_name')%>" />
<input id="race-date-<%= i %>" placeholder="Date of race?" type="text" value="<%= this.collection.models[i].get('race_date')%>" />
<input id="race-time-<%= i %>" placeholder="Time to finish race?" type="text" value="<%= this.collection.models[i].get('race_time')%>" />
</div>
<% } %>
<div class="col-md-3">
<input id="race-name-<%= this.collection.models.length %>" placeholder="Name of race?" type="text" />
<input id="race-date-<%= this.collection.models.length %>" placeholder="Date of race?" type="text" />
<input id="race-time-<%= this.collection.models.length %>" placeholder="Time to finish race?" type="text" />
</div>
</script>
</div> <!-- row -->
</div> <!-- /container -->
<script src="js/libs/jquery/jquery-min.js"></script>
<script src="js/libs/underscore/underscore-min.js"></script>
<script src="js/libs/backbone/backbone-min.js"></script>
<script src="js/libs/backbone/backbone.localStorage.js"></script>
<!-- <script src="js/libs/backbone/localStorage.js"></script> -->
<!-- <script src="js/libs/backbone/backbone-optamd3-min.js"></script> -->
<script src="js/models/racemodel.js"></script>
<script src="js/collections/races.js"></script>
<script src="js/views/raceview.js"></script>
<script src="js/app.js"></script>
</body>
在您的比赛视图中,您需要更改 addRace 函数以创建新的比赛模型。您只需不断更改相同的模型,而不是创建一个新模型。
你应该有一些这样的代码:
addRace: function() {
var race = new racemodel({
// code for model details
});
this.collection.add(race);
}
相关文章:
- 如何在本地存储(或其他数据库)上安全地保存敏感访问代码
- JavaScript-保存、存储和更新数组
- 在本地存储中保存并返回随机生成的字符串
- 如何保存和存储实时流中的音频
- 本地存储保存和检索文本输入值
- 表单选择不保存在本地存储中
- Javascript 会话存储变量未正确保存
- 没有从本地存储获取与保存的变量相同的类型
- 如何保存从单击this.nameClass动态设置的本地存储
- 如何将jQuery克隆的文本输入保存到cookie或html5存储中
- 如何在画布上绘制(使用鼠标/触摸)、保存绘制的对象、存储、加载和操作
- 无法使用HTML5 Web存储保存布尔值的解决方法
- 如何使用复选框启用选项并通过本地存储进行保存
- 将表数据保存在本地存储中
- 在保存到本地存储之前,如何修改我得到的 json 响应以使其看起来像这样
- 将图像从表单保存到本地存储并存储/加载它
- 存储日期选择器日期并保存在数据库中
- 在文本字段中显示保存的本地存储变量 - javascript
- 会话存储未保存或未加载
- 保存文本框数据并从本地存储返回 - angularJS -