1

我正在尝试将 profile.toJSON() 返回到一个对象,以便在上述代码之外使用它。我不完全了解主干函数的工作原理,所以我声明了一个全局变量 obj 并尝试使用 obj = profile.toJSON() 解析数据。当我使用 console.log(obj) 时,它会成功显示我的数据。当我将控制台放在上面的代码之外时,它会返回不足。

var obj; 
var ProfileView = Backbone.View.extend(
{       
    el: "#profiles",
    template: _.template($('#profileTemplate').html()),
    render: function(eventName) 
    {
        _.each(this.model.models, function(profile)
        {
            var profileTemplate = this.template(profile.toJSON());  
            obj = profile.toJSON();
            $(this.el).html(profileTemplate);
        }, this);

    return this;
    }           
});
4

2 回答 2

3

你把它弄错了。预先创建模型并将其传递给视图。不要试图从视图渲染代码中提取一些东西,它不应该以这种方式使用。

var Profile = Backbone.Model.extend({});
var ProfileCollection = Backbone.Collection.extend({
    model: Profile
});

var ProfileListView = Backbone.View.extend({
    ...
    // Everything render does is rendering
    render: function() {
        this.collection.each(function(model) {
            this.$el.append(
                this.template(model.toJSON);
            );
        }, this);
    }
    ...
});

// Your profile instance is defined outside the view, making 
// it de facto available to outside code
var profile = new Profile({
    name: 'Fere Res',
    rep: 48
});

// The profile we just created gets added to a collection
var profiles = new ProfileCollection([profile]);

// We create the profile list view and pass it the collection
var view = new ProfileListView({collection: profiles});

// When we render the view, the render() code defined above is called.
// You can easily see that all the params/variables it uses are in place
view.render();

// Rendering is done, let's check our model is still available
console.log(profile.toJSON()); // :)
于 2013-10-10T15:08:00.177 回答
0

我得到了这段代码,它实际上从 json 文件中获取数据:

$(function() {

    var Profile = Backbone.Model.extend({

        defaults: {
                tstamp: "",
                map:"",
                tagsCloud:"",
                sentiment: "",
                usersCloud: "",
                timeline: "",
                highlights: "",
                signals: ""
        },

        initialize: function() {

        }

    });

    var ProfileList = Backbone.Collection.extend({

                    model: Profile,
                    url: 'data.json'
    });   

    var ProfileView = Backbone.View.extend({

        el: "#profiles",
        template: _.template($('#profileTemplate').html()),
        render: function(eventName) {

        _.each(this.model.models, function(profile){
        var profileTemplate = this.template(profile.toJSON());

        //obj = profile.toJSON();
        //console.log(obj.tstamp);

        $(this.el).html(profileTemplate);
        }, this);

            return this;

        }


    });

        var profiles = new ProfileList();    
        var profilesView = new ProfileView({model: profiles});

        setInterval(function() {
                profiles.fetch({reset: true});
                }, 400); // Time in milliseconds
                profiles.bind('reset', function () {
                profilesView.render();
        }); 
       });

我尝试将配置文件添加到新集合中:

      var profiles1 = new ProfileList([profiles]);
      var view = new ProfileView({collection: profiles1});
      view.render();
      console.log(profile.toJSON()); 

我收到控制台消息:无法读取未定义的属性“模型”

于 2013-10-10T21:28:39.297 回答