2014-06-24 97 views
0

如何在Parse.find函數中全局訪問對象「上下文」?我收到一個錯誤「未捕獲的引用錯誤:上下文未定義」。我是backbone.js的新手。我看過幾篇關於骨幹觀點範圍的文章。但他們沒有幫助我。任何幫助將不勝感激。在骨幹視圖中訪問方法變量(解析視圖)

var MessageView = Parse.View.extend({ 

    el: "#post", 

    context:"", 

    tagName: "li", 

    initialize: function() { 
     var self = this; 
     this.render(); 
     console.log('Message View Initialized');  
    }, 
    render: function(res) { 
     //var context; 

     // declaring it here as a global variable doesn't help 

     var query = new Parse.Query(Message); 
    query.find({ 
    success:function(results){ 

     for(var i=0;i<results.length;i++){ 
      results[i].from=results[i].attributes.from; 
      results[i].message=results[i].attributes.messageBody; 
      message.set("from",results[i].from); 
      message.set("message",results[i].message); 
     } 

    context={from:message.attributes.from,message:message.attributes.message}; 

    //I want this object to be accessed anywhere in the render function 

    }, 
    error:function(error){ 
     console.log(error); 
    } 
    }); 
    var source=$("#messageTmpl").html(); 
    var messageTemplate = Handlebars.compile(source); 

    // I need to access context object in parse.find({}) here 
    // context variable with values "from" and "messageBody" 

    var html = messageTemplate(context); 
    console.log(context); 
    this.$el.html(html); 
}, 
}); 

我發現這個問題與我的非常相似。 [question] backbone view access methods variables

回答

0

將其聲明爲視圖屬性?

VAR =消息查看Parse.View.extend({

el: "#post", 

context:"", 

tagName: "li", 

query: new Parse.Query(Message), 

initialize: function() { 
    ... 
}, 
render: function(res) { 
    ... 
+0

我已宣佈它作爲視圖屬性,但我怎麼值傳遞給該視圖屬性?我想上下文對象傳遞給上下文視圖屬性現在我已經把一個空字符串 – user3769158

+0

你可以聲明爲視圖屬性,然後初始化,例如'initialize()'? –

+0

如何設置上下文視圖屬性與上下文對象?骨幹。 – user3769158