如何使用Backbone.Marionette.ItemView与胡须 [英] How to use Backbone.Marionette.ItemView with Mustache

查看:99
本文介绍了如何使用Backbone.Marionette.ItemView与胡须的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code工作正常使用 Backbone.Marionette.ItemView 而不是

The following code works fine using Backbone.Marionette.ItemView but not Mustache.

Backbone.Marionette.ItemView - 没有胡子

我想用同样的code,但加载使用模板varaible。

I would like to use the same code but loading the template varaible using Mustache.

下面是我的code:

Backbone.Marionette.ItemView - 带髭

任何想法,为什么我的code 不起作用,为什么?

Any idea why my code does not work and why?

感谢

推荐答案

木偶假定默认使用UnderscoreJS模板。简单地替换模板配置视图是不够的。您还需要更换渲染过程是如何工作的。

Marionette assumes the use of UnderscoreJS templates by default. Simply replacing the template configuration for a view isn't enough. You also need to replace how the rendering process works.

在您简单的例子,你只需要重写 Marionette.Renderer.render 函数来调用小胡子,然后设置模板您欣赏到字符串的模板,你想要的:

In your simple example, you only need to override the Marionette.Renderer.render function to call Mustache, and then set the template of your views to the string template that you want:


Backbone.Marionette.Renderer.render = function(template, data){
  return Mustache.to_html(template, data);
}

var rowTemplate = '{{ username }}{{ fullname }}';

// A Grid Row
var GridRow = Backbone.Marionette.ItemView.extend({
    template: rowTemplate,
    tagName: "tr"
});

请注意,您的jsfiddle仍然不会,即使你把这个code的地方,因为 GridView控件依然采用了jQuery选择/字符串作为工作模板属性。你需要与同类型模板函数来替换这个​​返回胡子。

Note that your JSFiddle still won't work even when you put this code in place, because the GridView is still using a jQuery selector/string as the template attribute. You'll need to replace this with the same type of template function to return mustache.

http://jsfiddle.net/derickbailey/d7qDz/

这篇关于如何使用Backbone.Marionette.ItemView与胡须的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆