javascript - 将数据传递到渲染 Backbone.js

标签 javascript json backbone.js

是否可以将 questions 变量传递到 View 渲染中?

我尝试在获取成功时调用 this.render 但出现错误,大概是因为 this.render 。不在正确的范围内。

app.AppView = Backbone.View.extend({
    initialize: function(){


        var inputs = new app.Form();

        inputs.fetch({

            success: function() {

                var questions = inputs.get(0).toJSON().Questions;

                app.validate = new Validate(questions);
                app.validate.questions();



            }, // End Success()

            error: function(err){
                console.log("Couldn't GET the service " + err);
            }

        }); // End Input.fetch()

        this.render();

    }, // End Initialize

    render: function(){
        el: $('#finder')
        var template = _.template( $("#form_template").html(), {} );
        this.$el.html(template);

    } 

最佳答案

使用与 View 实例不同的 this 对象调用成功回调。 解决这个问题的最简单方法是在调用 inputs.fetch 之前添加类似的内容:

var self = this;

然后在成功回调中:

self.render();

关于javascript - 将数据传递到渲染 Backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17807751/

相关文章:

backbone.js - Marionette .js : CompositeView: getItemView always returns 'undefined' on initialize

backbone.js - Handlebars 获取循环索引

javascript - RequireJS + Backbone 没有 jQuery?

javascript - 有没有办法阻止 Karma 提供缓存文件?

javascript - 带有 json 数据的动态图表

javascript - 我如何使用javascript在jsp中显示json数据

javascript - 使用 jQuery 将 HTML 文本抓取为 JSON,但由于循环引用而无法进行字符串化

javascript - indexOf() 函数返回 -1 值

javascript - 为什么添加商品的方法不正确?

json - 为什么我不能使用 JSONStore 获得一个简单的组合框在 EXTjs 4 中工作?