Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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

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
Templates 在带有下划线和主干线的模板中获取模型的子集合_Templates_Backbone.js_Requirejs_Underscore.js - Fatal编程技术网

Templates 在带有下划线和主干线的模板中获取模型的子集合

Templates 在带有下划线和主干线的模板中获取模型的子集合,templates,backbone.js,requirejs,underscore.js,Templates,Backbone.js,Requirejs,Underscore.js,我有这样一个JSon,它是一个问题集合: { "id": "505", "issuedate": "2013-01-15 06:00:00", "lastissueupdate": "2013-01-15 13:51:08", "epub": false, "pdf": true, "price": 3, "description

我有这样一个JSon,它是一个问题集合:

{
            "id": "505",
            "issuedate": "2013-01-15 06:00:00",
            "lastissueupdate": "2013-01-15 13:51:08",
            "epub": false,
            "pdf": true,
            "price": 3,
            "description": "Diese Version ist nur als PDF verfügbar.",
            "downloadable": true,
            "renderings": [
                {
                    "height": 976,
                    "width": 1024,
                    "sourcetype": "pdf",
                    "pagenr": 0,
                    "purpose": "newsstand",
                    "relativePath": null,
                    "size": 0,
                    "url": "a/url/image.jpg"
                },
                {
                    "height": 488,
                    "width": 512,
                    "sourcetype": "pdf",
                    "pagenr": 0,
                    "purpose": "newsstand",
                    "relativePath": null,
                    "size": 0,
                    "url": "a/url/image.jpg"
                }
}
我正在使用主干,我想访问子元素(渲染)。 我覆盖了parse方法,告诉我的“renderings”是一个新集合,这部分工作正常。 当我在模板中传递问题集合时,我遇到了一些问题。然后,我可以为每个问题创建一个视图,并访问其属性(id、issuedate、lastissueupdate等),但我如何访问问题的渲染

我的看法是:

define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/home/homeTemplate.html',
  'collections/issues/IssueCollection'
], function($, _, Backbone, homeTemplate, IssueCollection){

  var HomeView = Backbone.View.extend({
    el: $("#page"),

    initialize:function(){
      var that = this;


      var onDataHandler = function(issues){
        that.render();


      }
      this.issues = new IssueCollection();
      this.issues.fetch({
        success:onDataHandler, 
        error:function(){
          alert("nicht gut")
        }
      });
    },

    render: function(){

      $('.menu li').removeClass('active');
      $('.menu li a[href="#"]').parent().addClass('active');
      var compiledTemplate = _.template(homeTemplate, {_:_, issues: this.issues.models});
      this.$el.html(compiledTemplate);

    }

  });

  return HomeView;

});
然后我有一个非常基本的模板

要继续,我有一个包含渲染集合的模型集合问题,我想访问模板中的模型渲染

编辑: 发布集合

define([
    'underscore',
    'backbone',
    'models/issue/IssueModel'
], function(_, Backbone, IssueModel){

    var IssueCollection = Backbone.Collection.extend({

        model: IssueModel,

        url: function(){
            return '/padpaper-ws/v1/pp/fn/issues';
        },

        parse: function(response){
            return response.issues;
        }

        //initialize : function(models, options) {},

    });
    return IssueCollection;
});
define([
    'underscore',
    'backbone',
    'models/rendering/RenderingModel'
], function(_, Backbone, RenderingModel){

    var RenderingsCollection = Backbone.Collection.extend({

        model: RenderingModel

        //initialize : function(models, options) {},

    });
    return RenderingsCollection;
});
IssueModel

define([
  'underscore',
  'backbone',
  'collections/renderings/RenderingsCollection'
], function(_, Backbone, RenderingsCollection) {

  var IssueModel = Backbone.Model.extend({
    parse: function(response){
      response.renderings = new RenderingsCollection(response.renderings);
      return response;
     },
    set: function(attributes, options) {
      if (attributes.renderings !== undefined && !(attributes.renderings instanceof RenderingsCollection)) {
        attributes.renderings = new RenderingsCollection(attributes.renderings);
      }
      return Backbone.Model.prototype.set.call(this, attributes, options);
    }
  });
  return IssueModel;
});
define([
  'underscore',
  'backbone'
], function(_, Backbone) {

  var RenderingModel = Backbone.Model.extend({

  return RenderingModel;

});
渲染集合

define([
    'underscore',
    'backbone',
    'models/issue/IssueModel'
], function(_, Backbone, IssueModel){

    var IssueCollection = Backbone.Collection.extend({

        model: IssueModel,

        url: function(){
            return '/padpaper-ws/v1/pp/fn/issues';
        },

        parse: function(response){
            return response.issues;
        }

        //initialize : function(models, options) {},

    });
    return IssueCollection;
});
define([
    'underscore',
    'backbone',
    'models/rendering/RenderingModel'
], function(_, Backbone, RenderingModel){

    var RenderingsCollection = Backbone.Collection.extend({

        model: RenderingModel

        //initialize : function(models, options) {},

    });
    return RenderingsCollection;
});
渲染模型

define([
  'underscore',
  'backbone',
  'collections/renderings/RenderingsCollection'
], function(_, Backbone, RenderingsCollection) {

  var IssueModel = Backbone.Model.extend({
    parse: function(response){
      response.renderings = new RenderingsCollection(response.renderings);
      return response;
     },
    set: function(attributes, options) {
      if (attributes.renderings !== undefined && !(attributes.renderings instanceof RenderingsCollection)) {
        attributes.renderings = new RenderingsCollection(attributes.renderings);
      }
      return Backbone.Model.prototype.set.call(this, attributes, options);
    }
  });
  return IssueModel;
});
define([
  'underscore',
  'backbone'
], function(_, Backbone) {

  var RenderingModel = Backbone.Model.extend({

  return RenderingModel;

});
谢谢

您可以使用该方法对整个数据进行完整表示。不过,您必须使用
model.attributes.key
来访问模型的属性。所以我不太推荐它。相反,我认为您应该构建自己的JSON对象(递归地使用toJSON方法)。(如果不清楚,我将使用一些代码进行更新)

编辑:

var data = [], renderings;
for(var i=0; i<issues.length; i++) {
  data.push(issues.models[i].toJSON());
  data[i].renderings = [];
  renderings = issues.models[i].get('renderings');
  for(var j=0; j<renderings.length; j++) {
    data[i].renderings.push(renderings.models[j].toJSON());
  }
}
var data=[],渲染;

对于(var i=0;i实际上,我可以在我的模板中使用一个双精度的u,每个都是这样的,从视图中给出我的模型

<% _.each(issues, function(issue) { %>
    .... code ...
    <% _.each(issue.get('renderings'), function(rendering) { %>

……代码。。。
然后,要访问我的渲染属性,请执行以下操作:

<%= rendering.url %>

例如,不要像第一次那样“得到”


感谢您的参与!

我看不到模型渲染,这是渲染的父对象吗?我有一个JSon,它是一个问题集合:我想说它只是一个
问题,不是集合。@Loamhoof,这只是第一个问题,还有很多其他问题。@akoskm,基本上我的IssueModel中有解析方法d这将在我的“渲染”中放置一组渲染。我将更新代码。@brunettia我想是的,但它仍然令人困惑,很可能是整个集合,这没有多大意义。用[]来包装它。我不知道这到底能如何拯救我:)(我很新…)如果你能举一个小例子来访问每一个渲染,那将是非常好的!基本上,这段代码将为您提供问题的整个JSON表示,而不需要主干的内部内容。所以或多或少是您在问题中显示的JSON对象。只需测试它并
console.log(数据)。你会看到它是什么样子的。