My app is designed that router is changed then rendering other view but, I don't know that how communication for created other view object.
router.js
var app = app || {};
(function() {
'use strict';
var views = app.view = app.view || {};
app.Router = Backbone.Router.extend({
routes: {
'*home': 'homeRoute',
'about': 'aboutRoute',
'contact': 'contactRoute'
},
initialize: function() {
// create the layout once here
this.layout = new views.Application({
el: 'body',
});
},
homeRoute: function() {
var view = new views.Home();
this.layout.setContent(view);
},
aboutRoute: function() {
var view = new views.About();
this.layout.setContent(view);
},
contactRoute: function() {
var view = new views.Contact();
this.layout.setContent(view);
}
});
})();
view.js
var app = app || {};
(function() {
'use strict';
//views linitalize
var views = app.view = app.view || {};
views.Application = Backbone.View.extend({
initialize: function() {
// caching the jQuery object on init
this.$content = this.$('#content');
this.$loading = this.$('#loading');
},
setContent: function(view) {
var content = this.content;
if (content) content.remove();
this.showSpinner();
content = this.content = view;
console.log(view);
//content rednering
this.$content.html(content.render().el, this.hideSpinner());
},
showSpinner: function() {
this.$loading.show();
},
hideSpinner: function() {
this.$loading.hide();
},
});
views.Home = Backbone.View.extend({ });
views.About = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
var template = _.template("<strong>About page</strong>");
}
});
views.Contact = Backbone.View.extend({
my_template: _.template("<strong>Contact page</strong>");
});
})();
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<ul class="pills">
<li><a href="#/">Home</a></li>
<li><a href="#/about">About</a></li>
<li><a href="#/contact">Contact</a></li>
</ul>
<div id="content"></div>
<div id="loading">load the page...</div>
<div id="sumMenu"></div>
<script src="lib/jquery-3.1.1.min.js"></script>
<script src="lib/underscore.js"></script>
<script src="lib/backbone.js"></script>
<script src="lib/backbone.localstorage.js"></script>
<script src="routers/router.js" ></script>
<script src="views/view.js" ></script>
<script src="views/app.js" ></script>
</body>
</html>
If you look at this code, you'll understand. It's hard to communicate what I said above.
Look at the view.js, I wrote 3 views property (home
, about
, contact
) and those contained code that doesn't run.
my_template: _.template("<strong>Contact page</strong>");
I really want to know that after object create, how to set the objects value and rendering?