Backbone.js 处理数组属性 [英] Backbone.js Handling of Attributes That Are Arrays

查看:11
本文介绍了Backbone.js 处理数组属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢 Backbone,但我在做看似简单的事情时却遇到了最困难的时刻.感谢您对以下示例的任何帮助.

I really like Backbone, but I am having the hardest time doing what would seem to be simple things. I appreciate any help with the following example.

我有一个模型 Criteria,我想用它来存储 UI 中某些项目的状态.有几个简单的属性,其中一个属性是一组 ID,用于存储用户在 UI 中选择的标签 ID.

I have a model, Criteria, that I want to use to store the state of some items in my UI. there are a couple simple attributes, and one attribute that is an array of IDs used to store the IDs of tags the user has selected in the UI.

所以,我创建了一个新实例.我向标签数组添加了一些项目.然后,我想重新开始,创建一个新实例,分配给相同的变量.但是,我的标签数组继续保存我作为第一个 Criteria 实例的一部分添加到其中的信息.

So, I create a new instance. I add some items to the tags array. Then, I want to start fresh, create a new instance, assigned to the same variable. But, my tags array continues to hold information I added to it as a part of the first instance of Criteria.

我在下面记录了测试用例.

I have documented the test case below.

<!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>

我是不是有点离题了?我是否试图将 Backbone 用于非预期用途?还是我在 javascript OO 编程中遗漏了一些更一般的东西?

Am I just way off the mark here? Am I trying to use Backbone for things it was not intended? Or am I missing something more general in javascript OO programming?

附言我最初使用了一个 Backbone 标签集合,但这提出了一系列完全不同的问题,这些问题涉及在多个集合中引用一个标签模型,以及当一个项目从任何集合中删除时,Backbone 的 remove 方法如何取消设置集合"引用.改天,另一个问题.

P.S. I originally used a Backbone collection of tags, but that presented a whole different set of issues relating to having a Tag model referenced in multiple collections and how Backbone's remove method unsets the "collection" reference when an item is removed from any collection. Another day, another issue.

推荐答案

Thom Blake 关于为什么要为数组保留相同值的说法是正确的.解决此问题的一种选择是在初始化程序中设置默认值

Thom Blake is right about why it's keeping the same values for the array. one option for solving this is to set the default value in the initializer

        var Criteria = Backbone.Model.extend({

            defaults: {
                "status": "Normal",
                "priority": "Normal"
            },

            initialize: function(){
              if( !this.get('tags') ){ 
                this.set({tags: new Array()});
              }
            }

        });

这篇关于Backbone.js 处理数组属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆