简体   繁体   English

在单个Backbone视图和EJS模板中使用多个模型的问题

[英]Issues with using multiple models in a single Backbone view and EJS template

I'm trying to utilize two models in a single view and thus template but after attempting the examples shown in this question: Backbone.js: complex views combining multiple models I am running into some errors. 我试图在一个视图中使用两个模型,并在模板中使用两个模型,但是在尝试了此问题中显示的示例之后: Backbone.js:将多个模型组合在一起的复杂视图我遇到了一些错误。

First if I try to use a view model to combine both models as below: 首先,如果我尝试使用视图模型将以下两个模型组合在一起:

var model = new Backbone.Model();
model.set({ image: image, person: person });
var view = new Project.Views.Images.ShowView({ model: model });

I am unable to access anything in the template, each field is either empty or some are the string representation of a function. 我无法访问模板中的任何内容,每个字段为空或某些是函数的字符串表示形式。 Here's my template: 这是我的模板:

<img width="<%= image.width %>" height="<%= image.height %>" alt="<%= image.message %>" src="<%= image.url %>" />
<p><%= image.message %></p>
<h4>by <%= person.name %></h4>

Here's the output that template produces: 这是模板产生的输出:

<img width="" height="" alt="" src="function () {
  var base = getUrl(this.collection) || this.urlRoot || urlError();
  if (this.isNew()) return base;
  return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
}">
<p></p>
<h4>by </h4>

I have verified that both image and chaser are valid models and if I pass in only one of them to the view it works fine and binds that model to that view. 我已经验证了图像和追踪器都是有效的模型,如果仅将其中一个传递给视图,则它可以正常工作并将该模型绑定到该视图。

I have also tried the other approach: 我还尝试了另一种方法:

var view = new Project.Views.Images.ShowView({ model: image, person: person });

With the template then looking like this: 然后使用模板,如下所示:

<img width="<%= width %>" height="<%= height %>" alt="<%= message %>" src="<%= url %>" />
<p><%= message %></p>
<h4>by <%= person.name %></h4>

However this throws a javascript error saying person is undefined and if I remove person.name from the template the rest displays properly. 但是,这将引发一个JavaScript错误,指出人员未定义,如果我从模板中删除person.name,其余的将正确显示。

What am I doing wrong and are these even the right approaches to take? 我在做什么错,这些甚至是正确的方法吗?

There's nothing magical about the argument "model" in views, other then as a sort of convention, Backbone copies that into a "model" property in your view.[1] 视图中的参数“模型”没有什么神奇之处,除了作为一种约定外,Backbone将其复制到视图中的“模型”属性中。[1]

You can pass in as many things as you want to a view - you just need to do something with them in the initialize function. 您可以根据需要向视图传递尽可能多的内容-您只需要在initialize函数中对它们进行某些操作即可。

I would go with a riff on your 2nd choice: 我会在您的第二选择上采用即兴演奏:

var view = new Project.Views.Images.ShowView({ image: image, person: person });

in your view's initialize function 在您视图的初始化函数中

initialize: function(options) {
    this.image = options.image;
    this.person = options.person;
}

I'm not familiar with using EJS templates with Backbone, but usually, if you want a property of a model, you need to .get() it like model.get('propertyName') 我对将EJS模板与Backbone结合使用并不熟悉,但是通常,如果您想要模型的属性,则需要像model.get('propertyName')一样model.get('propertyName')进行.get() model.get('propertyName')

So, in the above case, you would instead do image.get('someImageProperty') and person.get('somePersonProperty') 因此,在上述情况下,您将改为执行image.get('someImageProperty')person.get('somePersonProperty')

In your view, there will be no "model" property, as you didn't pass in any argument named "model". 在您看来,将没有“模型”属性,因为您没有传入任何名为“模型”的参数。

If you wanted to go with your first choice, then you need to refer to the "sub-objects" via model.get('image').get('someImageProperty') 如果您想采用第一个选择,则需要通过model.get('image').get('someImageProperty')引用“子对象”

[1] What Backbone does by default with "model" is simply this: [1]骨干默认对“模型”执行的操作是这样的:

initialize: function(options) {
    this.model = options.model;
}

It does this, by default, for any options with these names: 默认情况下,它将对具有这些名称的所有选项执行此操作:

'model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName'

The EJS was wrong, when combining models into a single view model I had to use the following template (note the 'attributes' object): EJS是错误的,当将模型组合成单个视图模型时,我必须使用以下模板(请注意“ attributes”对象):

<img width="<%= image.attributes.width %>" height="<%= image.attributes.height %>" alt="<%= image.attributes.message %>" src="<%= image.attributes.url %>" />
<p><%= image.attributes.message %></p>
<h4>by <%= person.attributes.name %></h4>

I haven't looked into it deeper so I'm not sure why the JSON serialization is different with a single model but it is. 我没有更深入地研究它,所以我不确定为什么单个模型的JSON序列化会有所不同,但是确实如此。 When I just pass image in as the model I can omit the attributes object as so: 当我仅将图像作为模型传递时,可以这样忽略属性对象:

<%= width %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM