Kev*_*vin 35 javascript backbone.js
我真的很喜欢Backbone,但我最难做的事情看似简单.我感谢以下示例的任何帮助.
我有一个模型,标准,我想用来存储我的UI中的一些项目的状态.有一些简单属性,一个属性是一个ID数组,用于存储用户在UI中选择的标记的ID.
所以,我创建了一个新实例.我在tags数组中添加了一些项目.然后,我想重新开始,创建一个新实例,分配给同一个变量.但是,我的标签数组继续保存我作为第一个Criteria实例的一部分添加的信息.
我已经记录了下面的测试用例.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<script src="Scripts/Libraries/jquery-1.6.1.js" type="text/javascript"></script>
<script src="Scripts/Libraries/underscore.js" type="text/javascript"></script>
<script src="Scripts/Libraries/backbone.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function () {
// Simple model to hold some state about my UI.
var Criteria = Backbone.Model.extend({
defaults: {
"status": "Normal",
"priority": "Normal",
"tags": new Array()
}
});
// Create new criteria.
window.criteria = new Criteria();
// The length of the tags array should be 0. PASSES
console.log("Expect 0: Actual " + window.criteria.get("tags").length);
// Add a tag id to the tags array.
window.criteria.get("tags").push(5); // Tag with ID of 5.
// The length of the tags array should be 1. PASSES
console.log("Expect 1: Actual " + window.criteria.get("tags").length);
// Create a new instance of criteria.
window.criteria = new Criteria();
// The length of the tags array should be 0. FAILS
// CONFUSED. I thought this is now a new instance with a new set of attributes.
// Why does the tags collection still have an item in it.
console.log("Expect 0: Actual " + window.criteria.get("tags").length);
// OK. So, I will call the clear method on the model. This is supposed to remove all attributes
// from the model.
// Then, I will create it again.
window.criteria.clear();
window.criteria = new Criteria();
// The length of the tags array should be 0. FAILS. Still 1.
console.log("Expect 0: Actual " + window.criteria.get("tags").length);
// ARGH!
console.log("HELP!");
});
</script>
</head>
<body>
<h1>Test</h1>
<p>Backbone test page.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我在这里是不是很划算?我是否尝试使用Backbone来实现它的目的?或者我在javascript OO编程中遗漏了一些更通用的东西?
PS我最初使用Backbone标签集合,但是它提出了一系列不同的问题,这些问题与多个集合中引用的Tag模型有关,以及当从任何集合中删除项目时,Backbone的remove方法如何取消设置"集合"引用.另一天,另一个问题.
btf*_*ord 70
"默认值"也可以是一个函数.
var Criteria = Backbone.Model.extend({
defaults: function () {
return {
"status": "Normal",
"priority": "Normal",
"tags": new Array()
}
}
});
Run Code Online (Sandbox Code Playgroud)
这将在实例化新Criteria时创建新数组.请参阅:http://backbonejs.org/#Model-defaults
Der*_*ley 34
汤姆布莱克说的是为什么它保持阵列的相同值.解决此问题的一个选项是在初始化程序中设置默认值
var Criteria = Backbone.Model.extend({
defaults: {
"status": "Normal",
"priority": "Normal"
},
initialize: function(){
if( !this.get('tags') ){
this.set({tags: new Array()});
}
}
});
Run Code Online (Sandbox Code Playgroud)
Tam*_*ake 13
在"默认值"下定义"标记"时,您将创建一个新数组并将其设置为该类的默认值.然后,当您创建一个新实例时,它具有相同的Array引用,它仍然包含您推送到它的内容.
而不是为标签设置默认值,您应该能够[]在第一次使用它之前将其设置为:
window.criteria = new Criteria()
window.criteria.set({'tags', []}) //you can use new Array() if you want
window.criteria.get('tags').push(5)
window.criteria = new Criteria()
console.log(window.criteria.get('tags')) //should be undefined
window.criteria.set({'tags', []})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16472 次 |
| 最近记录: |