骨干木偶和ICanHaz(胡子)模板配置 [英] Backbone Marionette and ICanHaz (Mustache) templates configuration

查看:111
本文介绍了骨干木偶和ICanHaz(胡子)模板配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迁移骨干基本应用木偶,我想用 ICanHaz.js 作为模板系统(基于髭)。

我使用的是AMD和Require.js的唯一途径,使ICanHaz.js它和骨干的工作是利用jvashishtha的的版本


我第一次纯粹的骨干风格实现的应用程序。

特别是我用来加载每个模板作为与 Require.js文本插件原始字符串,然后添加模板ICH的对象。这在创建对象ICH的方法具有装载模板的同一个名字:

I'm migrating a Backbone basic app to Marionette and I would like to use ICanHaz.js as a template system (based on Mustache).
I'm using AMD and Require.js and the only way to make ICanHaz.js working with it and Backbone was to use jvashishtha's version.

I first implemented the app in pure Backbone style.
In particular I used to load each template as raw strings with the Require.js' text plugin and then add the template to the ich object. This create a method in ich object that has the same name of the loaded template:

define([ 
'jquery',
'underscore',
'backbone',
'iCanHaz',
'models/Job',
'text!templates/Job.html'

], function( $, _, Backbone, ich, Job, jobTemplate) {     
    var JobView = Backbone.View.extend({
        render: function () {
             /* since the render function will be called iterativetly  
                at the second cycle the method ich.jobRowTpl has been already defined
                so ich will throw an error because I'm trying to overwrite it.
                That is the meaning of the following if (SEE: https://github.com/HenrikJoreteg/ICanHaz.js/issues/44#issuecomment-4036580)
             */
            if (_.isUndefined(ich.jobRowTpl)){ 
                ich.addTemplate('jobRowTpl', jobTemplate);
            };

            this.el = ich.jobRowTpl(this.model.toJSON());
            return this;
        }
    });
    return JobView;

});

到目前为止好。这个问题带有木偶。

在previous code的骨干版本工作正常,但也没有必要来定义木偶的观点渲染功能。

木偶假定默认使用UnderscoreJS模板。别人已经问<一个href=\"http://stackoverflow.com/questions/11084021/how-to-use-backbone-marionette-itemview-with-mustache\">how使用与胡子木偶。

从这个问题的答案我试图实现我的解决方案。在app.js:

So far so good. The problem comes with Marionette.
The previous code works fine in the Backbone version, but there is no need to define a render function in Marionette's views.
Marionette assumes the use of UnderscoreJS templates by default. Someone else has already asked how to use Mustache with Marionette.
From that answer I've tried to implement my solution. In app.js:

app.addInitializer(function(){

  //For using Marionette with ICH
  var templateName=''; //not sure if this would help, anyway it makes no difference
  Backbone.Marionette.Renderer.render = function(template, data){
    if (_.isUndefined(ich.template)){ 
          //this defines a new template in ich called templateName
          ich.addTemplate(templateName, template);
    };
    return ich.templateName(data);
  }

但控制台抛出我:

But the console throws me:

Uncaught TypeError: Object #<Object> has no method 'templateName' 

所以模板尚未定义。无论如何,任何暗示,如果我在正确的方向我?

So the template has not been defined. Anyway, any hint if I'm in the right direction?

推荐答案

我认为这只是你的渲染函数中的JS有问题。

I think it's just a problem with your JS inside your renderer function.

这行:

返回ich.templateName(数据);

return ich.templateName(data);

将寻找一个字面上叫TEMPLATENAME模板

Will look for a template literally called "templateName"

你想要什么,因为TEMPLATENAME是一个变量是一样的东西:

What you want, since templateName is a variable is something like:

return ich[templateName](data);

那么它将间$ P $私人的TEMPLATENAME变量的值来代替。

Then it will interprete the value of the templateName variable instead.

这篇关于骨干木偶和ICanHaz(胡子)模板配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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