我試圖通過將視圖和模型聲明分離爲模塊(Browserify)來創建一個最小功能的Backbone.js應用程序。我的代碼:在Browserify模塊中的骨幹視圖 - 不工作
的index.html
<head>
<script src="bundle.js"></script>
</head>
<body>
<div id="myDiv">Loading...</div>
</body>
<script type="text/template" id="myTemplate">
Test Content
</script>
Model.js
var Backbone = require('backbone')
Backbone.$ = $
Model = Backbone.Model.extend({
});
module.exports = Model;
View.js
var _ = require('underscore')
var $ = require('jquery')
var Backbone = require('backbone')
Backbone.$ = $
View = Backbone.View.extend({
el: '#myDiv',
template: _.template($('#myTemplate').html()),
initialize: function(){this.el.append(this.$el.html(this.template()));},
});
module.exports = View;
main.js
var _ = require("underscore");
var Backbone = require("backbone");
var $ = require("jquery");
var View = require("./View");
var Model = require("./Model");
Backbone.$ = $;
$(document).ready(function(){
var model = new Model();
var view = new View({model: model});
});
Browserify在控制檯:
browserify main.js -o bundle.js -d
錯誤:
Uncaught TypeError: Cannot read property 'replace' of undefined
一個想法 - 在你看來,你使用'this.el.append' ...你的意思是'this。$ el.append'?我不熟悉應用於原始DOM元素的'append'。 – arbuthnott