如何在 ajax 调用时执行控制器方法?
How to execute controller methods on ajax call?
在Marionette或Backbone中,当导航字段中的url发生变化时执行路由。有什么方法可以在 ajax 调用上执行路由?
如果您想触发路线更改,您可以执行以下操作,但不推荐这样做:
yourRouterInstance.navigate("your_url_that_is_mapped",{trigger:true});
相反,你需要解耦路由导航、数据加载和视图创建,也能达到同样的效果。
- 创建控制器 JS 对象。
用 Backbone.Events
扩展此对象。
var AppController = _.extend({},Backbone.Events);
在此对象上创建方法并将它们绑定到事件。
3.1 每个方法都会通过 ajax
加载所需的适当数据。
3.2 创建一个合适的view
.
实例
3.3 将加载的数据传递给view
的实例
3.4 调用navigate
方法并将url更新为合适的url。
AppController.on("any_custom_event",function(payload) {
// MAKE THE AJAX CALL.
// AFTER SUCCESSFUL CALL INSTANTIATE THE VIEW AND PASS THE DATA .
// CALL yourAppRouter.navigate("your_current_url",{trigger:false});
});
从路由回调/需要的地方触发此自定义事件。
//in router callbacks
AppController.trigger("any_custom_event",payload);
// In other places where required like inside an ajax success call back:
AppController.trigger("any_custom_event",payload);
来自 Backbone 文档:
navigate
router.navigate(fragment, [options])
Whenever you reach a point in your application that you'd like to save as a URL, call navigate in order to update the URL. If you wish to also call the route function, set the trigger option to true
. To update the URL without creating an entry in the browser's history, set the replace option to true
.
因此,在 ajax 回调中,您可以执行类似
的操作
$.ajax({
success: function () {
app.navigate('path/to/your/route', {trigger: true})
}
})
或者使用原生 JS(到处都支持),您可以自己更改哈希值:
$.ajax({
success: function () {
window.location = '#your/route/path/';
}
})
那也会触发路由路径。
两种解决方案都会将路线保存在浏览器的后台历史记录中。
在Marionette或Backbone中,当导航字段中的url发生变化时执行路由。有什么方法可以在 ajax 调用上执行路由?
如果您想触发路线更改,您可以执行以下操作,但不推荐这样做:
yourRouterInstance.navigate("your_url_that_is_mapped",{trigger:true});
相反,你需要解耦路由导航、数据加载和视图创建,也能达到同样的效果。
- 创建控制器 JS 对象。
用
Backbone.Events
扩展此对象。var AppController = _.extend({},Backbone.Events);
在此对象上创建方法并将它们绑定到事件。
3.1 每个方法都会通过ajax
加载所需的适当数据。
3.2 创建一个合适的view
.
实例 3.3 将加载的数据传递给view
的实例 3.4 调用navigate
方法并将url更新为合适的url。AppController.on("any_custom_event",function(payload) { // MAKE THE AJAX CALL. // AFTER SUCCESSFUL CALL INSTANTIATE THE VIEW AND PASS THE DATA . // CALL yourAppRouter.navigate("your_current_url",{trigger:false}); });
从路由回调/需要的地方触发此自定义事件。
//in router callbacks AppController.trigger("any_custom_event",payload); // In other places where required like inside an ajax success call back: AppController.trigger("any_custom_event",payload);
来自 Backbone 文档:
navigate
router.navigate(fragment, [options])
Whenever you reach a point in your application that you'd like to save as a URL, call navigate in order to update the URL. If you wish to also call the route function, set the trigger option to
true
. To update the URL without creating an entry in the browser's history, set the replace option totrue
.
因此,在 ajax 回调中,您可以执行类似
的操作$.ajax({
success: function () {
app.navigate('path/to/your/route', {trigger: true})
}
})
或者使用原生 JS(到处都支持),您可以自己更改哈希值:
$.ajax({
success: function () {
window.location = '#your/route/path/';
}
})
那也会触发路由路径。
两种解决方案都会将路线保存在浏览器的后台历史记录中。