Backbone:如何使用单个命令更新所有模型的属性

发布于 2024-12-11 03:41:33 字数 177 浏览 0 评论 0原文

我正在创建一个简单的文件夹列表,单击这些文件夹时将被标记为选中,即它们的模型将其 selected 属性更改为 true。我希望此选择是排他的 - 在将文件夹标记为选择之前,所有其他文件夹应标记为未选择。

是的,我能想到的是循环遍历集合来更改每个模型的属性。有没有一些更简单的方法可以完成这个任务?

I am creating a simple list of folders which when clicked will be marked selected that is their model will have its selected property changed to true. I want this selection to be exclusive - all other folders should be marked unselected before a folder is marked selected.

Right all I can think of looping through the collection to change the property of every model. Is there some easier way I can accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-12-18 03:41:33

在我正在进行的一个项目中,我们在集合级别维护“选定”项目。我们添加了 getCurrent()setCurrent(model) 方法,并且集合将引发 change:selection 事件。这对我们来说非常有效。

--编辑:每个请求的代码示例。--

以下是我们为所有集合扩展的 base.collection.js 的一部分。您会注意到,我们可以根据 id 或实际模型设置 current,因此 col.setCurrent(123)col.setCurrent(anInstanceOfAModel) 都可以工作。同时为当前模型调用 setCurrent 不会触发更改事件

    setCurrent: function (id)
    {
        var isModel = !(_.isString(id) || _.isNumber(id));

        var triggerChange = this._setupCurrent(isModel
                                ? id
                                : this.get(id));

        if (triggerChange)
            this.trigger("change:current", this._current);

        return this.getCurrent();
    },

    getCurrent: function ()
    {
        return this._current;
    },

    _setupCurrent: function (current)
    {
        var old = this._current;
        this._current = current;

        if (current && old && old.id == current.id)
            return false;

        return true;
    },

In a project I'm working on we maintain the 'selected' item at the collection level. We have added getCurrent() and setCurrent(model) methods and the collection will raise a change:selection event. This has worked real well for us.

--EDIT: code sample per request.--

The following is part of our base.collection.js which we extend for all our collections. You'll notice that we can set current based on id or the actual model so col.setCurrent(123) and col.setCurrent(anInstanceOfAModel) both work. Also calling setCurrent for a model that is already the current doesn't fire the change event

    setCurrent: function (id)
    {
        var isModel = !(_.isString(id) || _.isNumber(id));

        var triggerChange = this._setupCurrent(isModel
                                ? id
                                : this.get(id));

        if (triggerChange)
            this.trigger("change:current", this._current);

        return this.getCurrent();
    },

    getCurrent: function ()
    {
        return this._current;
    },

    _setupCurrent: function (current)
    {
        var old = this._current;
        this._current = current;

        if (current && old && old.id == current.id)
            return false;

        return true;
    },
〆一缕阳光ご 2024-12-18 03:41:33

您还可以有一个“状态”对象,在其中存储最后选择的文件夹。

然后,您可以将该集合更改回未选中状态,而无需循环遍历整个集合。

You can also have a "state" object, in which you store the last selected folder.

Then you change that one back to not selected, without having to loop through the entire collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文