私はbackbone.jsとrequire.jsの初心者です。このボイラープレートを使用することにしました。従来の html ページのリンク クリックのように、2 つのビューを接続したいと考えています。したがって、最初のビューのコードは次のようになります。
// View.js
define(["jquery", "backbone", "models/Model", "text!templates/heading.html"],
function($, Backbone, Model, template){
var View = Backbone.View.extend({
// The DOM Element associated with this view
el: ".example",
// View constructor
initialize: function() {
// Calls the view's render method
this.render();
},
// View Event Handlers
events: {
},
// Renders the view's template to the UI
render: function() {
// Setting the view's template property using the Underscore template method
this.template = _.template(template, {});
// Dynamically updates the UI with the view's template
this.$el.html(this.template);
// Maintains chainability
return this;
}
});
// Returns the View class
return View;
}
);
HTML テンプレート:
<!-- HTML Template -->
<h3>My first backbone app</h3>
<a href="#next">NEXT PAGE</a>
そして最後にルーター:
define(["jquery", "backbone", "models/Model", "views/View", "collections/Collection"],
function($, Backbone, Model, View, Collection) {
var DesktopRouter = Backbone.Router.extend({
initialize: function() {
// Tells Backbone to start watching for hashchange events
Backbone.history.start();
},
// All of your Backbone Routes (add more)
routes: {
// When there is no hash on the url, the home method is called
"": "index",
"next": "next"
},
index: function() {
// Instantiates a new view which will render the header text to the page
new View();
},
next: function() {
// Instantiates next view
new NextView();
}
});
// Returns the DesktopRouter class
return DesktopRouter;
}
);
私の質問は、ビュー NextView を定義するコードをファイル View.js に入れる方法ですか? 助けてくれてありがとう。