Javascript 如何处理转换到
我有以下代码,它在输入搜索查询并按下enter按钮或单击submit按钮时调用TransitionRoute(“搜索”) 但是,我的路由器仍然不会在模板中显示searchQuery,其中显示:Javascript 如何处理转换到,javascript,jquery,ember.js,Javascript,Jquery,Ember.js,我有以下代码,它在输入搜索查询并按下enter按钮或单击submit按钮时调用TransitionRoute(“搜索”) 但是,我的路由器仍然不会在模板中显示searchQuery,其中显示: 您搜索了:“{{searchQuery}}” URL看起来像http://www.example.com/#/search/[object object]在搜索某个东西时(我觉得这不合适) (完整代码可在以下位置查看:) 以下是相关代码: 模板: <script type="text/x-handl
您搜索了:“{{searchQuery}}”
URL看起来像http://www.example.com/#/search/[object object]
在搜索某个东西时(我觉得这不合适)
(完整代码可在以下位置查看:)
以下是相关代码:
模板:
<script type="text/x-handlebars" data-template-name="container">
<button {{action "doSearch"}} rel="tooltip-bottom" title="search" class="icon"><i class="icofont-search"></i></button>
{{view Ember.TextField valueBinding="search" action="doSearch"}}
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="searchpage">
<h1>Search</h1>
{{#linkTo "home"}}Homepage{{/linkTo}}
<p>You searched for: "{{searchQuery}}"</p>
</script>
以及Searchpage路线:
MyApp.SearchRoute = Ember.Route.extend({
setupController: function(controller) {
controller.set('searchQuery', this.get('query'));
},
renderTemplate: function() {
this.render('searchpage', { into: 'container' });
}
});
首先,您需要在路由器中为搜索路由定义动态段:
MyApp.Router.map(function() {
this.route("home", { path: "/" });
this.route("search", { path: "/search/:query" })
});
然后在doSearch
操作中设置应用程序的searchQuery
属性。您还可以将query
变量传递给transitionRoute
方法,因为它将填充动态段
MyApp.ApplicationController = Ember.Controller.extend({
// the initial value of the `search` property
search: '',
doSearch: function() {
// the current value of the text field
var query = this.get('search');
this.set('searchQuery', query);
this.transitionToRoute('search', query);
}
});
由于需要从App.SearchController
实例访问此属性,因此需要使用needs
API将两个控制器连接在一起:
MyApp.SearchController = Ember.Controller.extend({
needs: ['application'],
application: Ember.computed.alias('controllers.application')
});
将controllers.application
属性别名为justapplication
,以避免在模板中键入过多内容
然后在搜索
模板中绑定到此属性:
<script type="text/x-handlebars" data-template-name="searchpage">
<h1>Search</h1>
{{#linkTo "home"}}Homepage{{/linkTo}}
<p>You searched for: "{{application.searchQuery}}"</p>
</script>
这将从URL获取参数,并使用query
键的值设置应用程序控制器
差不多了,希望我没有错过任何东西 首先,您需要在路由器中为搜索路由定义动态段:
MyApp.Router.map(function() {
this.route("home", { path: "/" });
this.route("search", { path: "/search/:query" })
});
然后在doSearch
操作中设置应用程序的searchQuery
属性。您还可以将query
变量传递给transitionRoute
方法,因为它将填充动态段
MyApp.ApplicationController = Ember.Controller.extend({
// the initial value of the `search` property
search: '',
doSearch: function() {
// the current value of the text field
var query = this.get('search');
this.set('searchQuery', query);
this.transitionToRoute('search', query);
}
});
由于需要从App.SearchController
实例访问此属性,因此需要使用needs
API将两个控制器连接在一起:
MyApp.SearchController = Ember.Controller.extend({
needs: ['application'],
application: Ember.computed.alias('controllers.application')
});
将controllers.application
属性别名为justapplication
,以避免在模板中键入过多内容
然后在搜索
模板中绑定到此属性:
<script type="text/x-handlebars" data-template-name="searchpage">
<h1>Search</h1>
{{#linkTo "home"}}Homepage{{/linkTo}}
<p>You searched for: "{{application.searchQuery}}"</p>
</script>
这将从URL获取参数,并使用query
键的值设置应用程序控制器
差不多了,希望我没有错过任何东西 不是完整答案,但您需要从应用程序控制器中检索您的搜索词,就像在我的叉形小提琴中一样:我想将其标记为答案,但我也想知道如何解决[object]URL而不是完整答案,但是您需要从应用程序控制器中检索您的搜索词,就像在我的叉形小提琴中一样:我想把它标记为一个答案,但我也想知道如何解决[object]URL非常感谢!你非常清楚地解释了它是如何工作的,从现在起我可以自己做:)Yw。最后一点注意:我可能会在
App.SearchController
上保留搜索相关属性,并在App.ApplicationRoute
上保留doSearch
操作。然后,我将使用需求API从应用程序控制器中引用搜索控制器。(基本上颠倒了当前的关系。)这将使您可以在应用程序中的任何位置使用doSearch
操作,而无需进一步设置,此外还可以将搜索相关逻辑保留在一个(明显的)位置。非常感谢!你非常清楚地解释了它是如何工作的,从现在起我可以自己做:)Yw。最后一点注意:我可能会在App.SearchController
上保留搜索相关属性,并在App.ApplicationRoute
上保留doSearch
操作。然后,我将使用需求API从应用程序控制器中引用搜索控制器。(基本上颠倒了当前关系。)这将使您可以在应用程序中的任何位置使用doSearch
操作,而无需进一步设置,此外还可以将搜索相关逻辑保留在一个(明显的)位置。