骨干木偶和ICanHaz(胡子)模板配置

编程入门 行业动态 更新时间:2024-10-24 16:25:17
本文介绍了骨干木偶和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: github/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模板。别人已经问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(胡子)模板配置

本文发布于:2023-11-06 21:41:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:木偶   胡子   骨干   模板   ICanHaz

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!