English 中文(简体)
以助手方法为主
原标题:backbone toJSON with helper methods

I have a backbone model with attributes and some helper methods that output something other than the actual attribute (for formatting for example).

然而,当我打电话toJSON时,只归还这些属性,因此,我必须使用的模板能够进入这些助手方法。 是否有办法解决这一问题? 或者,我是否应当采取不同的做法?

究竟是围绕这种方式制定一种形式化的特性,每当确定变化时加以更新吗?

最佳回答

豪尔赫将采用我自己的方法,把新增加的json列入模板。

同样:

var userModel = Backbone.Model.extend({
    initialize: function(){
        _.bindAll(this,  fullname ,  toFullJSON );
    },
    fullname: function(){
        return this.get( name ) + " " + this.get( lastname );
    },
    toFullJSON: function(){
        var json = this.toJSON();
        return _.extend(json, {fullname : this.fullname()});
    }
});

var user = new userModel();
u.set({name:  John , lastname:  Doe });

// you will see in this console log, that the toFullJSON function returns both the toJSON properties, and your added propert(y)(ies)...
console.log(u.toFullJSON());
问题回答

确保初专干事是正确的。 如果您是返回物体,可在其中提供一些背后参考资料(在JSON中不支持这些物品,并且可能省略)。





相关问题
iteration on couchapp

i had write this on mustache.html on couchapp **{{%IMPLICIT-ITERATOR iterator=i}} {{#example}} hallo {{i}} {{/example}}** with this array { "example": ["alpha","beta","gamma","delta"] } but ...

how do I get my mustache.js template file included?

I m working with mustache.js for the first time. All the examples I m finding seem to talk about putting everything inline, but I want my templates in external files so they can be used in multiple ...

Mustache partials and code reuse

I m getting the hang of mustache for a project I ve started during the weekend. I m using the PHP implementation. I have, however a couple of inquiries as I m not used to the system. How do you ...

Timeout in a Couchapp list when using mustache

I have a simple list view in which I (try to) use mutache to render the output of a list containing 5 results. function(head, req) { var row, mustache = require("vendor/couchapp/lib/mustache....

Is there anything I can t do with mustache?

I m evaluating http://github.com/janl/mustache.js and I m thinking about how it will work in general over time with a time. If I just build a giant object, is mustache sufficient to transform it into ...

热门标签