Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 可以传递主干集合以查看错误字符串不是函数_Javascript_Backbone.js_Underscore.js_Underscore.js Templating - Fatal编程技术网

Javascript 可以传递主干集合以查看错误字符串不是函数

Javascript 可以传递主干集合以查看错误字符串不是函数,javascript,backbone.js,underscore.js,underscore.js-templating,Javascript,Backbone.js,Underscore.js,Underscore.js Templating,我是第一次尝试将集合直接发送到视图模板,但失败了。表示字符串不是函数的类型错误。我知道我正确地返回了我的收藏,也许我以错误的方式将我的收藏传递到了人们的视野中。不管怎样,我会很感激你的帮助 这里是我的视图代码 var EncoursView = Backbone.View.extend({ el: "#contentEncours", //Container div template: "tpl/EncoursView.html", initialize: function (

我是第一次尝试将集合直接发送到视图模板,但失败了。表示字符串不是函数的类型错误。我知道我正确地返回了我的收藏,也许我以错误的方式将我的收藏传递到了人们的视野中。不管怎样,我会很感激你的帮助

这里是我的视图代码

var EncoursView = Backbone.View.extend({
   el: "#contentEncours", //Container div
   template: "tpl/EncoursView.html",
   initialize: function () {
                  console.log('Encours View Initialized');
                  this.collection.fetch();
                  console.log(this.collection);
               },
   render: function () {
                  $(this.el).empty();
                   var that = this;
                  //Fetching the template contents
                  $.get(this.template({lists:this.collection}), function (template) {
                    that.$el.html(template); //adding the template content.
                  }, 'html');

                  return that;
           }
});
这是我试图迭代我的集合的视图

<script>
_.templateSettings = {
    evaluate: /\{\{(.+?)\}\}/g,
    interpolate: /\{\{=(.+?)\}\}/g,
    escape: /\{\{-(.+?)\}\}/g
};
    {{ _.each(items, function(item) { }}
        <ul>
            <li>Isin: {{= item.isinCode }}</li>
        </ul>
    {{ }); }}
</script>

我猜这是:

$.get(this.template({lists:this.collection}), function (template) {
    that.$el.html(template); //adding the template content.
}, 'html');
用于从服务器检索模板源(
tpl/EncoursView.html
),填写它,然后将其插入视图的
el

这里有几个问题

  • this.template
    是当您试图像函数一样调用它时的
    'tpl/EncoursView.html'
    字符串

  • 需要URL作为其第一个参数

  • $.get
    回调中,
    template
    应该是模板源代码(当然,一旦您达到了这个程度)

  • 您的模板:

    {{ _.each(items, function(item) { }}
        <ul>
            <li>Isin: {{= item.isinCode }}</li>
        </ul>
    {{ }); }}
    
    模板看起来和现在一样


    调用将为您提供一个对象数组,这有助于您避免意外更改模板内的任何内容。这是主干网的标准做法。

    1)渲染在哪里调用?2) 当将数据传递给模板时,将其作为JSON loke coll.toJSON()传递,我在帖子中添加了渲染调用它在路由中的调用,我使用rest服务将我的数据转换为JSON格式,我认为我不需要调用.toJSON()collection.fetch是异步的,你不能这样调用渲染,我该如何调用它?
    {{ _.each(items, function(item) { }}
        <ul>
            <li>Isin: {{= item.isinCode }}</li>
        </ul>
    {{ }); }}
    
    $.get(this.template, function(template) {
        var tmpl = _.template(template);
        var html = tmpl({ items: that.collection.toJSON() });
        // ---------------^^^^^------------------^^^^^^^^
    }, 'html');