2013-10-10 89 views
2

我想將profile.toJSON()返回給一個對象,以便在上面的代碼之外使用它。我不完全理解骨幹函數是如何工作的,所以我聲明瞭一個全局變量obj並試圖用obj = profile.toJSON()解析數據。當我使用console.log(obj)時,它會成功顯示我的數據。當我把控制檯放在上面的代碼之外時,它返回undefined。返回變量外骨幹函數

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; 
    }   
}); 
+0

要在哪裏使用它? – ronnyfm

+0

我想解析另一個JavaScript文件varible obj。 –

+0

這是因爲你的外部'console.log'在渲染有機會發生之前被調用。 – thibauts

回答

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()); // :) 
+0

你是對的,我複製了一些代碼,而沒有考慮太多。糾正。 – thibauts

+0

謝謝。這讓我想起了收藏每一次我都會忘記。 – thibauts

+0

謝謝,終於明白了! –

2

我已經得到這個代碼實際上獲取從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()); 

我已經得到了控制檯消息:無法讀取未定義的屬性「模型」