客户端仅在属性骨干 [英] Client-side only attributes in Backbone

查看:116
本文介绍了客户端仅在属性骨干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种模式的一个相当通用模型和收集(见下文),我处理的,作为一系列意见的基础。在若干的意见,车型之一的选择产生动作(通过'选择'属性),我需要能够跟踪的选择上不仅仅是客户端。

然而,似乎没有干净的方式在骨干做到这一点。 /添加到任何属性模型上改变了客户机上将同步时间达到服务器。我不能使用 {沉默:是} 改变这种属性的时候,因为我需要触发我的意见变化的时候,变更该属性事件触发。我已经拿出来做到这一点的唯一方法是覆盖在 Backbone.Model

保存功能>

我的问题:有没有办法让客户端的唯一属性,我丢失或者是我的方法中,我只是没有看到其他方式结构上有缺陷

  VAR CSEvent = Backbone.Model.extend({
        idAttribute:_id
        urlRoot:'/ API /事件',
        默认值:{
            标题:,
            类型:本地,
            repeatOrOneTime:一次性
            选择:假的
        }
    });    VAR CSEventCollection = Backbone.Collection.extend({
        型号:CSEvent,
        网址:'/ API /事件',
        getSelectedEvent:功能(){
            返回this.find(功能(csevent){返回csevent.get('选择')===真;});
        },
        selectEvent:函数(事件ID){
            this.deselectEvent();
            this.get(事件ID).SET({选择:真});
        },
        deselectEvent:功能(){
            。this.getSelectedEvent()集({选择:假});
        }
    });


解决方案

尝试重写 Model.toJSON()方法,你可以在看的骨干示范code 这种方法也不是很复杂。另外,官方文档建议重写它在特殊需要的情况下。

尝试是这样的:

  VAR CSEvent = Backbone.Model.extend({
  的toJSON:功能(){
    返回_.clone(_.pick(this.attributes,称号,类型,repeatOrOneTime));
  }
});

I have a fairly generic model and collection of that model (see below) I am dealing with as the basis for a series of views. On several of the views, the selection of one of the models generates actions (via the 'selected' attribute), and I need to be able to keep track of the selection on just the client side.

However, it appears that there is no clean way to do this in Backbone. Any attributes added to/changed on the model on the client will be sync'd up to the server. I can't use {silent : yes} when changing that attribute because I need to trigger changes in my views when the change event fires on that attribute. The only way I have come up with to do this is to overwrite the save function on Backbone.Model

My question: is there a way to have client-side only attributes that I am missing OR is my approach structurally flawed in some other way that I am just not seeing?

    var CSEvent = Backbone.Model.extend({
        idAttribute: "_id",
        urlRoot : '/api/events',
        defaults: {
            title : "",
            type : "Native",
            repeatOrOneTime : "OneTime",
            selected : false
        }
    });    

    var CSEventCollection = Backbone.Collection.extend({
        model: CSEvent,
        url: '/api/events',
        getSelectedEvent : function() {
            return this.find(function(csevent) { return csevent.get('selected') === true; });
        },
        selectEvent : function(eventId) {
            this.deselectEvent();
            this.get(eventId).set({selected : true});
        },
        deselectEvent : function() {
            this.getSelectedEvent().set({selected : false});
        }
    });

解决方案

Try to override the Model.toJSON() method, as you can see in the Backbone Model code this method is not very complicate. Also the official documentation suggests to override it in case of special needs.

Try something like this:

var CSEvent = Backbone.Model.extend({
  toJSON: function(){
    return _.clone( _.pick( this.attributes, "title", "type", "repeatOrOneTime" ) );
  }
});

这篇关于客户端仅在属性骨干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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