在Backbone视图中访问方法变量(解析视图)

时间:2014-06-24 00:23:44

标签: javascript backbone.js parse-platform

如何访问对象" context"在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);   
},
});

我发现这个问题与我的问题非常相似。 [问题] backbone view access methods variables

1 个答案:

答案 0 :(得分:0)

将其声明为视图属性?

var MessageView = Parse.View.extend({

el: "#post",

context:"",

tagName:  "li",

query: new Parse.Query(Message),

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