处理 underscore.js 模板中未定义的属性

发布于 2025-01-07 18:00:53 字数 1321 浏览 5 评论 0原文

我正在视图中渲染单个 Backbone 模型。我使用默认的下划线模板来渲染模型。当我渲染视图时(尽管模型尚未加载),如何处理“未定义”属性错误?为了澄清这一点,这里有一个例子。

// Using Mustache style markers
_.templateSettings = {
    interpolate : /\{\{(.+?)\}\}/g
};

App.Model = Backbone.Model.extend({});

App.Collection = Backbone.Collection.extend({
    model: App.Model
});

App.View = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'render');
        this.template = _.template($('#model_template').html());
        this.model.bind('reset', this.render);
    },
    render: function() {
        var renderedContent = this.template(this.model.toJSON());
        $(this.el).html(renderedContent);
        return this;
    }
});

// HTML
<div id="container"></div>
<script id="model_template" type="text/template">
    <strong>Name:</strong> {{ name }} <br/>
    <strong>Email:</strong> {{ email }} <br/>
    <strong>Phone:</strong> {{ phone }}
</script>

// Run code
var collection = new App.Collection;
var view = new App.View(model: collection.at(0));
$('#container').html(view.render().el);
collection.fetch();

运行此代码时,视图将呈现两次,第一次使用空模型,第二次在 AJAX 查询完成时呈现(并且触发“重置”)。但我面临的问题是,当模型为空时,JS 在第一个实例中停止。它给出一个错误,指出模型属性未定义。

我在这里做错了什么?当视图在第一个实例中呈现时,我可以抑制“未定义”错误吗?

I am rendering a single Backbone model in a view. I am using the default underscore template to render the model. How do I handle the "undefined" attribute errors when I am rendering the view (though the model has not loaded)? To clarify, here is an example.

// Using Mustache style markers
_.templateSettings = {
    interpolate : /\{\{(.+?)\}\}/g
};

App.Model = Backbone.Model.extend({});

App.Collection = Backbone.Collection.extend({
    model: App.Model
});

App.View = Backbone.View.extend({
    initialize: function() {
        _.bindAll(this, 'render');
        this.template = _.template($('#model_template').html());
        this.model.bind('reset', this.render);
    },
    render: function() {
        var renderedContent = this.template(this.model.toJSON());
        $(this.el).html(renderedContent);
        return this;
    }
});

// HTML
<div id="container"></div>
<script id="model_template" type="text/template">
    <strong>Name:</strong> {{ name }} <br/>
    <strong>Email:</strong> {{ email }} <br/>
    <strong>Phone:</strong> {{ phone }}
</script>

// Run code
var collection = new App.Collection;
var view = new App.View(model: collection.at(0));
$('#container').html(view.render().el);
collection.fetch();

When this code is run, the view is rendered twice, first with an empty model and second when the AJAX query is complete (and 'reset' is triggered). But the issue I am facing is JS stops at the first instance when the model is empty. It gives an error saying the model attribute is undefined.

What am I doing wrong here? Can I suppress the 'undefined' error when the view is rendered in the first instance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

や三分注定 2025-01-14 18:00:53

一种选择是为模型定义默认空值。

App.Model = Backbone.Model.extend({
  defaults: {
    name: '',
    email: '',
    phone: ''
  }
});

One option is to define default empty values for your model.

App.Model = Backbone.Model.extend({
  defaults: {
    name: '',
    email: '',
    phone: ''
  }
});
儭儭莪哋寶赑 2025-01-14 18:00:53

您的操作可能会出现一些问题:

当您这样做时:

// Run code
var collection = new App.Collection;
var view = new App.View({model: collection.at(0)});
$('#container').html(view.render().el);
collection.fetch();

首先,您创建集合。它还没有任何与之关联的模型。因此,下一步时,您将使用集合中尚不存在的第一个模型来初始化视图。
正确的方法是 fetch() 集合,完成后创建视图。类似于:

var p = collection.fetch();
p.done(function () {
  var view = new App.View({model: collection.at(0)});
  ...
}

最好将容器元素提供给视图。你可以简单地做到这一点

var view = new App.View({model: collection.at(0), el: '#container'});

There are a few things going wrong with what you do:

When you do:

// Run code
var collection = new App.Collection;
var view = new App.View({model: collection.at(0)});
$('#container').html(view.render().el);
collection.fetch();

First, you create the collection. It does not yet have any models associated with it. So when next you initialize the view with the collection's first model that does not yet exist.
The correct way to do that is to fetch() the collection and when that has completed create the view. Something like:

var p = collection.fetch();
p.done(function () {
  var view = new App.View({model: collection.at(0)});
  ...
}

It is also better to give the container element to the view. You can do that by simply

var view = new App.View({model: collection.at(0), el: '#container'});
你与昨日 2025-01-14 18:00:53

为了避免渲染下划线模板时出现未定义变量错误,请将模板数据传递到包装对象内。缺少属性访问不会引发错误。

var template = _.template($('#template').html());
var data = {name:'Jeo'}; // phone not available
template({data:data});
// Output: Jeo 

<div id="template">
    {{data.name}} {{data.phone}}
</div>

To avoid the undefined variable error while rendering underscore template, pass the template data inside a wrapper object. Missing property access won't throw an error.

var template = _.template($('#template').html());
var data = {name:'Jeo'}; // phone not available
template({data:data});
// Output: Jeo 

<div id="template">
    {{data.name}} {{data.phone}}
</div>
抚你发端 2025-01-14 18:00:53

我还建议使用远端模板

I'd also suggest using Distal templates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文