必要になるまで jQuery セレクターを遅らせることができます。
render: function(){
var templateHtml = $(this.template).html();
_.template(templateHtml);
}
または、ビューの初期化時にセレクターを実行できます。
initialize: function(){
this.template = _.template($(this.template).html());
}
または、コードをそのままにして、ビューを定義するときにセレクターを評価させたい場合は、アプリコード全体を初期化するときに呼び出す関数ですべてのバックボーンコードをラップできます...$(function(){}
実際の HTML ページの jQuery関数やbeforeEach
Jasmine テストの関数など:
MyApp = (function(){
var myApp = {};
myApp.MyView = Backbone.View.extend({
template: _.template($("#item-template").html())
// ...
});
return myApp;
});
次に、アプリでこれを開始します。
$(function(){
var myApp = MyApp();
new myApp.MyView();
// ...
});
そしてあなたのジャスミンテストで:
describe("how this thing works", function(){
beforeEach(function(){
var myApp = MyApp();
this.view = new myApp.MyView();
// ...
});
});
これらのソリューションのいずれかを用意したら、Jasmine-jQuery などを使用してフィクスチャをロードできます。
FWIW: 必要に応じて、これらの手法を組み合わせて使用する傾向があります。