-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
backbonejs #6
Comments
Lessons learned
|
Router如何以编程的方式添加新的route var AppRouter = Backbone.Router.extend({
routes: {
"": 'showDashboard',
'/test/me': function(){},
"*actions": "defaultRoute"
},
defaultRoute: function(actions){},
testMeAgain: function(){}
});
// Initiate the router
var app_router = new AppRouter;
app_router.on('route:defaultRoute', function(actions) {
alert(actions);
});
// 以下这句没起作用, 原因是通过route方法添加的route会放到routes的最后,
// 也就是放在了*actions的最后,所以没起作用,
// 解决办法是把*actions从routes中移除, 然后以编程的方式放在下面这句的后面。
app_router.route('/test/me/again', 'testMeAgain');
// Start Backbone history a necessary step for bookmarkable URL's
Backbone.history.start(); 如果console.log(app_router)会发现并没有这个对象中并没有全部的routes,这是因为route方法将新的route放到了Backbone.history里边。见这里 route: function(route, name, callback) {
// ...
Backbone.history.route(route, _.bind(function(fragment) {
//...
}, this));
return this;
}, |
继承测试index.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
<script type="text/javascript" src="bower_components/underscore/underscore-min.js"></script>
<script type="text/javascript" src="bower_components/backbone/backbone.js"></script>
</head>
<body>
<div id="content">
</div>
<script type="text/javascript">
var ListView = Backbone.View.extend({
el: '#content',
number: 5,
initialize: function () {
this.render();
},
render: function(){
this.$el.html(this.getNumber());
},
getNumber: function(){
return this.number;
}
});
var CustomListView = ListView.extend({
number: 6
});
var view = new CustomListView();
</script>
</body>
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
模板代码:
Model
View
Collection
Router
History
Sync
可以在Model或Collection中覆盖sync方法如下:
I typically need to override backbone's sync method when I need to only sync certain attributes. A typical implementation looks like this:
这里有lcoalStorage是如何重写Sync方法的:
http://backbonejs.org/docs/backbone.localStorage.html
The text was updated successfully, but these errors were encountered: