【问题标题】:Backbone model toJSON does not render all attributes骨干模型 toJSON 不渲染所有属性
【发布时间】:2014-04-13 15:57:32
【问题描述】:

我发现了这样的问题:Backbone model .toJSON() doesn't render all attributes to JSON by model.set

但他们没有解决。

我也有类似的问题。这是一个示例代码:

Animal = Backbone.Model.extend({
    ...
    ...,
    initStats:function(){
        this.attributes.stats = [];
        var that = this;
        var type = new Species();
        type.store.findAll(type, function(tx, res){
            var base = JSON.parse(res.rows.item(that.attributes.did-1).value);
            that.set("name",base.name);
            that.set("text", base.text);
            return that;
        }, function(tx,err){console.log(err)}, "");
    }
});

type.store.findAll 是 Species 类中的一个函数,它连接到 DB 并返回所有物种。

我在另一个页面上:

mym = new Animal();
    mym.initStats().save(); //see below
    mym.store.findAll(mym, function(tx,res){
        for(var i = 0;i < res.rows.length; i++){
            console.log(res.rows.item(i).value);
        }
    }, function(tx,err){console.log(err)}, "");

同样,Animal.store.findAll 是一个查找所有动物的函数。 Animal.save()的SQL生成函数如下:

create: function (model,success,error,options) {
        //when you want use your id as identifier, use apiid attribute
        if(!model.attributes[model.idAttribute]) {
            // Reference model.attributes.apiid for backward compatibility.
            var obj = {};

            if(model.attributes.apiid){
                obj[model.idAttribute] = model.attributes.apiid;
                delete model.attributes.apiid;
            }else{
                obj[model.idAttribute] = guid();
            }            
            model.set(obj);
        }

        var colNames = ["`id`", "`value`"];
        var placeholders = ['?', '?'];
        var params = [model.attributes[model.idAttribute], JSON.stringify(model.toJSON())];
                    console.log(model);
                    console.log(model.toJSON());
        this.columns.forEach(function(col) {
            colNames.push("`" + col.name + "`");
            placeholders.push(['?']);
            params.push(model.attributes[col.name]);
        });
        var orReplace = WebSQLStore.insertOrReplace ? ' OR REPLACE' : '';
                    //console.log("INSERT" + orReplace + " INTO `" + this.tableName + "`(" + colNames.join(",") + ")VALUES(" + placeholders.join(",") + ");");
        this._executeSql("INSERT" + orReplace + " INTO `" + this.tableName + "`(" + colNames.join(",") + ")VALUES(" + placeholders.join(",") + ");", params, success, error, options);

控制台输出是:(我将只输入字符串化的 JSON)

s {cid:“c575”,属性:对象,_changed:false,_previousAttributes:对象,更改:对象......} {"name":null, "text":null, "id":"5475b1e0-9d6c-48d1-e090-cb84e4e84ca6"} {"name":null, "text":null, "id":"5475b1e0-9d6c-48d1-e090-cb84e4e84ca6"}

id 由模型中的其他函数生成。第一行是模型(toJSON 之前),第二行是 JSON 对象,第三行是 SQL 选择结果。实际打印顺序为三行。

我不知道为什么名称和文本为空。在控制台输出中,它们在 s.attributes 下都有值。

【问题讨论】:

    标签: javascript sql json backbone.js


    【解决方案1】:

    假设 findAll 函数是异步的,你怎么知道 mym.initStats().save(); 中的 save() 没有发生 initStats 中的 findAll 完成之前?

    【讨论】:

    • save() 调用 create() 函数,在该函数中我让控制台同时记录模型和 JSON 字符串。该模型具有在 initStats() 中设置的属性,但 JSON 字符串没有。所以我确实认为 findAll() 发生在 save() 之前,但是当模型转换为 JSON 时出现了一些问题。毕竟,save() 是从 findAll() 中调用的; @erturne
    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多