BackboneベーシックアプリをMarionetteに移行していますが、 ICanHaz.jsをテンプレートシステム(Mustacheベース)として使用したいと思います。
私はAMDとRequire.jsを使用しており、ICanHaz.jsをそれで動作させる唯一の方法とBackboneはjvashishthaのバージョンを使用することでした。
私は最初に純粋なバックボーンスタイルでアプリを実装しました。特に、 Require.jsのテキストプラグイン
を使用して各テンプレートを生の文字列としてロードしてから、テンプレートをichオブジェクトに追加していました。これにより、ロードされたテンプレートと同じ名前のichオブジェクトにメソッドが作成されます。
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;
});
ここまでは順調ですね。問題はマリオネットにあります。
前のコードはバックボーンバージョンで正常に機能しますが、マリオネットのビューでレンダリング関数を定義する必要はありません。
Marionetteは、デフォルトでUnderscoreJSテンプレートの使用を想定しています。他の誰かがすでにマリオネットで口ひげを使用する方法を尋ねています。
その答えから、私は自分のソリューションを実装しようとしました。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);
}
しかし、コンソールは私を投げます:
Uncaught TypeError: Object #<Object> has no method 'templateName'
したがって、テンプレートは定義されていません。とにかく、私が正しい方向にいる場合のヒントはありますか?