如何将ParseQuery的结果推送到JavaScript中的模板属性?

如何将ParseQuery的结果推送到JavaScript中的模板属性?,javascript,parse-platform,handlebars.js,Javascript,Parse Platform,Handlebars.js,我能够检索查询结果,但似乎无法将它们推送到模板中 BlogApp.Views.Blog = Parse.View.extend({ template: Handlebars.compile($('#blog-tpl').html()), className: 'blog-post', render: function() { var self = this, attributes = this.model,

我能够检索查询结果,但似乎无法将它们推送到模板中

BlogApp.Views.Blog = Parse.View.extend({
    template: Handlebars.compile($('#blog-tpl').html()),
    className: 'blog-post',

    render: function() {
        var self = this,
            attributes = this.model,
            query = new Parse.Query(BlogApp.Models.Blog);
        query.include("author");

        attributes.loggedIn = Parse.User.current() != null;
        if (attributes.loggedIn) {
            attributes.currentUser = Parse.User.current().toJSON();
        }

        console.log(attributes.objectId);

        var Post = Parse.Object.extend("Post");
        var commentsQuery = new Parse.Query(Post);
        commentsQuery.equalTo("senderParseObjectId", attributes.objectId);
        commentsQuery.find({
            success: function(comments) {
                console.log(comments);
            }
        });

        attributes.comment = $comments;
        this.$el.html(this.template(attributes));

    }

});
模板:


我将结合使用将从解析检索到的数据呈现到视图中。这是一个单页应用程序。

不太了解解析,但看起来您需要在异步查找请求的成功处理程序中呈现模板-否则它不会等待此结果,而是呈现属性数据减去查找请求的结果

BlogApp.Views.Blog = Parse.View.extend({
  template: Handlebars.compile($('#blog-tpl').html()),
  className: 'blog-post',

  render: function() {
    var self = this,
        attributes = this.model,
        query = new Parse.Query(BlogApp.Models.Blog);
    query.include("author");

    attributes.loggedIn = Parse.User.current() != null;
    if (attributes.loggedIn) {
        attributes.currentUser = Parse.User.current().toJSON();
    }

    console.log(attributes.objectId);

    var Post = Parse.Object.extend("Post");
    var commentsQuery = new Parse.Query(Post);
    commentsQuery.equalTo("senderParseObjectId", attributes.objectId);
    commentsQuery.find({
        success: function(comments) {
            console.log(comments);
            attributes.comment = comments;
            self.$el.html(self.template(attributes))
        }
    });

  }

});

有道理。但是如何做到这一点而不出现未捕获的类型错误是一个棘手的问题!如您所说,将其纳入成功回调的范围。
BlogApp.Router = Parse.Router.extend({

    start: function() {
        Parse.history.start({
            root: '/beta/'
        });
    },

    routes: {
        '': 'index',
        'post/:url': 'blog',
        'admin': 'admin',
        'login': 'login',
        'store': 'store',
        'reset': 'reset',
        'logout': 'logout',
        'add': 'add',
        'register': 'register',
        'editprofile': 'editprofile',
        'changeprofilepic': 'changeprofilepic',
        ':username': 'userprofile'
    },

    blog: function(url) {
        BlogApp.fn.setPageType('blog');
        BlogApp.query.blog.equalTo("objectId", url).find().then(function(blog) {
            var model = blog[0];
            var author = model.get('author');
            model = model.toJSON();
            model.author = author.toJSON();

            BlogApp.fn.renderView({
                View: BlogApp.Views.Blog,
                data: {
                    model: model,
                    comment: $comments
                }
            });

            BlogApp.blog = blog[0];
        });
    },

});
BlogApp.Views.Blog = Parse.View.extend({
  template: Handlebars.compile($('#blog-tpl').html()),
  className: 'blog-post',

  render: function() {
    var self = this,
        attributes = this.model,
        query = new Parse.Query(BlogApp.Models.Blog);
    query.include("author");

    attributes.loggedIn = Parse.User.current() != null;
    if (attributes.loggedIn) {
        attributes.currentUser = Parse.User.current().toJSON();
    }

    console.log(attributes.objectId);

    var Post = Parse.Object.extend("Post");
    var commentsQuery = new Parse.Query(Post);
    commentsQuery.equalTo("senderParseObjectId", attributes.objectId);
    commentsQuery.find({
        success: function(comments) {
            console.log(comments);
            attributes.comment = comments;
            self.$el.html(self.template(attributes))
        }
    });

  }

});