- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个游戏模型,其记分卡属性是一个集合。我正在嵌套此集合,因此当我初始化时,我使用 NestCollection 创建更改处理程序以保持所有内容更新和同步。每当我创建新的游戏模型时,都会将一个空模型添加到记分卡属性集合中,但仅在内存中 - 保存到本地存储的内容是正确的。我不明白为什么。
这是我的游戏模型定义 - 注意控制台日志语句结果:
var Game = Backbone.Model.extend({
localStorage: new Backbone.LocalStorage('datastore'),
defaults: {
name : '',
scorecards: new ScorecardList(),
created : 0
},
initialize : function() {
console.log(this.scorecards); // prints undefined
console.log(this.get('scorecards')); // length is 0 as expected
this.scorecards = nestCollection(this, 'scorecards', new ScorecardList(this.get('scorecards')));
console.log(this.scorecards); // length is 1, with empty element in it
console.log(this.get('scorecards')); // length is 0 as expected
if (this.isNew()) this.set('created', Date.now());
}
});
嵌套代码:
function nestCollection(model, attributeName, nestedCollection) {
//setup nested references
for (var i = 0; i < nestedCollection.length; i++) {
model.attributes[attributeName][i] = nestedCollection.at(i).attributes;
}
//create empty arrays if none
nestedCollection.bind('add', function (initiative) {
if (!model.get(attributeName)) {
model.attributes[attributeName] = [];
}
model.get(attributeName).push(initiative.attributes);
});
nestedCollection.bind('remove', function (initiative) {
var updateObj = {};
updateObj[attributeName] = _.without(model.get(attributeName), initiative.attributes);
model.set(updateObj);
});
return nestedCollection;
}
这是我用来创建新游戏的代码:
addGame: function () {
var g = new Game({
name:this.ui.gameName.val()
});
app.gameList.create(g,{wait:true});
//Backbone.history.navigate('game/new/'+ g.id, true);
}
最佳答案
您的问题来自这段代码:
new ScorecardList(this.get('scorecards'))
在这里,您为 ScorecardList
构造函数提供另一个集合作为参数。这个集合恰好是一个对象。因此,您的集合的构造函数会认为它是您提供给它来创建模型的对象。
所以基本上, this.get('scorecards'))
被转换为 Scorecard
(或任何你的模型被称为),这就是为什么你有一个空的型号。
将参数传递给构造函数用于与创建集合不同的目的是一个坏主意,您应该在之后调用一个方法。
关于javascript - 为什么我的主干集合包含空模型项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18812731/
我开发了一个 jquery &backbone.js 网络应用程序。 一个组件有一个 html 表,该表后面是一个主干.js 集合。 此集合中的任何更改都应该导致 html 表的更新,所以我写 thi
我创建了一个由 HTTP Basic 保护的无状态 RESTful API。 在客户端,我将创建一个登录表单,向/auth-check 发送请求以检查凭据,并根据状态代码,将这些凭据存储在客户端并使用
我已经有一个具有经典结构的存储库 /branches /current /beta /tags /1.0 /2.0 /trunk 我想从当前的编辑器切换到 Eclipse,但我应该创建
我正在阅读 red bean SVN book ,它说要像这样布局主干、分支和标签文件夹: $ svn list file:///var/svn/multi-project-repo project-
当特定属性发生变化时,Backbone 模型触发自定义事件的好方法是什么? 到目前为止,这是我得到的最好的: var model = Backbone.Model.extend({ initi
我想避免在 Backbone.Collection 上的每个“添加删除重置”事件上重新渲染。 所以在 Backbone.View 中我可能有: this.listenTo(this.collectio
这一定是我没有正确理解的东西。 我有一个名为 Album 的 Backbone 模型: var Album = Backbone.Model.extend({ idAttribute: "_id",
假设我有一个具有一堆 bool 属性的 Backbone 模型: Car = Backbone.Model.extend({}); car_one = new Car({ a_c: true,
我从 collection.fetch() 调用中得到一个包含多个嵌套对象的对象。有没有办法解析这些子对象并将它们作为单独的模型传递到集合中? 这是我要返回的数据的示例。从服务器获取时,我想将它作为
有一个脚本(js),我希望在它上面运行,比如$(document).ready()。有一种方法可以在模板(例如 home_tpl.html)文件中编写脚本。但我确信这不是一个好方法。 我正在从主干 V
配合我正在尝试从主干集合中包含的所有模型中获取更大的属性。 举个例子: App.Models.Example = Backbone.Model.extend({ defaults: {
我遇到这样一种情况,我需要为一个人保存几个对象,但一次保存一个对象,并且在获取时,我将所有对象作为一个数组以及该人的信息。 这是模型的样子: //Model for one save { pe
我开始阅读一些 Backbone 教程,发现一件让我惊讶的事情。为什么模型是在 View 内部创建的?如果我想要一个模型有两个不同的 View (我认为 MVC 的用途)怎么办?假设我需要一个模型 C
我使用backbone.js 创建了一个小型网站。它托管在 IIS 7 中。 路由: routes: { "/": "index", "": "ind
我现在正在构建一个 View ,当我单击 .organization 链接时,我想触发我的编辑事件,但是单击此元素时,没有任何内容被触发,我无法理解为什么。 这是构建我的 View 的代码, App.
我有一些全局参数,我想在每次调用集合上的提取时发送这些参数...我的问题是我不想声明 data: { ... }每次我获取。 有没有一种方法可以在 Collection 本身内部提供默认参数,并可以添
Backbone 新手在这里。我想开始在 Web 应用程序上使用主干(后端是 Ruby on Rails),直到现在它的客户端功能还很少(一些用于执行 slideToggles 的 jquery,以及
我想知道是否有办法从其中一个模型中获取对集合的引用。例如,如果下面集合中的任何人以某种方式知道属于一个集合或多个集合。 Fiddle (function() { window.App = {
如何在集合获取中阻止模型的解析功能? $(function() { var Task = Backbone.Model.extend({ url : function(
我们一直在使用位于文件夹根目录中的存储库,一切都很好。然而,自从网站上线后,我们需要对一些工作进行分支,但由于缺少主干而无法这样做。 我的问题是,如何在当前目录结构中创建一个主干并将所有当前内容移动到
我是一名优秀的程序员,十分优秀!