如何访问不同控制器的功能? - JavaScript MVC
假设我有 2 个控制器来处理页面的 2 个部分 - 标题和页面内容。大多数操作发生在 pagecontent.js(页面内容的控制器)中。它有一组加载各种视图的函数。但标题有后退按钮。我跟踪历史记录,当单击后退按钮时,我想调用 pagecontent.js 上的适当函数来加载特定视图。这是我想要做的示例:
pagecontent.js
$.Controller('Controller.Pagecontent', {
...
".home click": function(){
this.loadHome();
},
loadHome: function(){
$('#pagecontent').html('//views/home', {});
}
...
})
header.js
$.Controller('Controller.Header', {
...
".back click": function(){
if( HISTORY[0] == 'home' )
// call loadHome() from pagecontent.js
}
...
})
我不想将所有这些函数复制到 header.js,因为它会是多余的,并且某些函数具有复杂的逻辑。
有什么想法吗?
Assume I have 2 controllers that handle 2 parts of the page - header and page content. Most of the actions happen in pagecontent.js (controller for page content). It has set of functions to load various views. But header has the back button. I track the history, and when the back button is clicked, I want to call the appropriate function on pagecontent.js to load particular view. Here's the example of what I'm trying to do:
pagecontent.js
$.Controller('Controller.Pagecontent', {
...
".home click": function(){
this.loadHome();
},
loadHome: function(){
$('#pagecontent').html('//views/home', {});
}
...
})
header.js
$.Controller('Controller.Header', {
...
".back click": function(){
if( HISTORY[0] == 'home' )
// call loadHome() from pagecontent.js
}
...
})
I don't want to copy all those functions to header.js because it will be redundant and some functions have complex logic.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
任何在 JavaScript 中定义的对象都可以从浏览器页面中的任何 JavaScript 代码访问。
如果您在同一页面中加载两个
.js
文件,尽管在不同部分,一个文件中的对象可能可以在另一个中访问,因此这些文件可以在window
对象中定义。file1.js
file2.js
如果 file2.js 在 file1.js 之后加载,则会显示“a=13”的警报。每当您在加载两个文件之前调用
show_b()
时,都会显示带有“b=14”的文件。为了避免干扰,您可以使用
windows.a
和windows.b
而不是a
和b
。同一示例适用于函数定义。
Any defined object in javascript may be accesible from any javascript code in browser's page.
If you load two
.js
files in the same page although in different parts objects in one may be accesible in the other so those may be defined inwindow
object.file1.js
file2.js
The alert with 'a=13' will be shown if file2.js is loaded after file1.js. The one with 'b=14' will be shown whenever you call
show_b()
before both files are loaded.To avoid interferences you can use
windows.a
andwindows.b
instead ofa
andb
.The same example works with function definitions.
共享代码的方法有很多,但 JavaScriptMVC 中惯用的方法是这样的:
其中
#pageContent
是您首先用来创建控制器的选择器。更好的是使用自定义事件。如果 pageContent 是 header 的父级,则可以使用 jQuery.trigger:
或者可以使用 OpenAjax 事件:
There are lots of ways to share code, but the idiomatic way in JavaScriptMVC would be something like:
where
#pageContent
is the selector you used to create the controller in the first place.Even better would be to use a custom event. You can use jQuery.trigger if pageContent is a parent of header:
Or you can use an OpenAjax event:
如果每个页面的 js 代码有所不同,并且如果较少,则使用内联 js 或将这些代码行添加到单独的文件中并将其包含在布局中(假设您有不同的布局,以便不会在任何地方加载相同的 js 文件) )
If js code is going to differ per page and if it is less then make use of inline js or add those lines of code in separate file and include it inside layout (assuming you have different layout so that same js file is not getting loaded everywhere)
您可以使用原型访问其他控制器功能/事件。例如。在这种情况下,它会是这样的:
You can access other controllers functions/events using prototype. Eg. in this situation, it would be like this: