I have been looking into Ember.js, and it looks really great, but one thing that concerns me, and that I can't get my mind around it, is if I start using it on an already running project.
我一直在调查余烬。js,看起来很不错,但有一件事让我担心,我不能把它放在心上,那就是如果我开始在一个已经在运行的项目上使用它。
Will I eventually have to move everything client side, and make my application a single page application at some point?
我最终是否必须将所有的客户端都移动,并使我的应用程序在某个时候成为单个页面应用程序?
let me clarify...
让我澄清…
So far the best way to communicate between client and server using Ember is REST. and that looks great, but what I don't like is having all the templates loaded for the first time. and moving all the logic in my server to the client (or am I getting all of this wrong?), cause it looks like my server side will become a logic-less REST API.
到目前为止,使用Ember在客户机和服务器之间进行通信的最佳方式是REST。看起来不错,但我不喜欢第一次加载所有模板。并且将服务器中的所有逻辑移动到客户端(或者是我做错了吗?),因为看起来我的服务器端将成为一个无逻辑的REST API。
Also, I'm using Yii Framework which has some JavaScript (Ajax enabled) components like grids. how can I have ember interact with all of this on navigation without having to rewrite a bunch of stuff already working on my application?
此外,我正在使用Yii框架,它具有一些JavaScript (Ajax支持)组件,如网格。我怎样才能让余烬在导航中与所有这些交互,而不必重写已经在我的应用程序上工作的一堆东西呢?
I'm on the login page (or state), and then after login in, I have to display a grid, that is just easy with Yii, and a full page load, but If I am using Ember, how can I have my grid display as it normally would? do I have to pre-load a handlebar template for the grid, and also the JavaScript that controls it?
我在登录页面(或状态)上,然后在登录之后,我必须显示一个网格,这对Yii来说很简单,并且有一个完整的页面装载,但是如果我使用烬,我如何能像往常一样显示网格?我是否需要为网格预加载一个handlebar模板,以及控制它的JavaScript ?
3 个解决方案
#1
10
No, you should not move everything to client side, especially authentication and validation that could be bypassed otherwise.
不,您不应该将所有内容都转移到客户端,特别是在其他情况下可以绕过的身份验证和验证。
What you move to Emberjs is the yii-s View part of MVC, controller will output i.e JSON.
你移动到Emberjs的是MVC的yii-s视图部分,控制器会输出i。e JSON。
That Data then gets mapped to Embers Model part through Ember routing and controllers etc.
然后,这些数据通过余烬路由和控制器等被映射到余烬模型部分。
Since you are replacing Yii's presentation logic with ember you should not use Yii's UI classes like CGridView. Mixing them might be possible but that does not seem like a good idea. You have to run your own in Ember.
由于您正在替换Yii的表示逻辑,所以您不应该使用Yii的UI类,比如CGridView。混合它们可能是可能的,但这似乎不是一个好主意。你必须在烬里运行你自己的。
http://www.yiiframework.com/wiki/409/ember-js-with-yii-rest-backend-demo-application/
http://www.yiiframework.com/wiki/409/ember-js-with-yii-rest-backend-demo-application/
#2
7
Just to add a Yii perspective here. A lot of the "magic" of grid views/lists happens within the data provider (for complex searching, sorting and filtering) and by having the data formatted with parsed fields on models.
在这里添加一个Yii透视图。网格视图/列表的许多“魔力”发生在数据提供程序(用于复杂的搜索、排序和过滤)内,并通过模型上的解析字段对数据进行格式化。
So you could utilise the same concepts server side and just output the final JSON, paginated and all, from within your own widget; or even just override the grid view and output json rather than the view after all the data/configuration processing.
所以你可以使用相同的概念服务器端输出最终的JSON,分页等等,从你自己的小部件中;甚至只是覆盖网格视图并输出json,而不是所有数据/配置处理之后的视图。
Once you've got JSON down rather than HTML it's very easy to replicate the front end of the grid, there's really not much functionality going on there.
一旦你有了JSON而不是HTML,就很容易复制网格的前端,那里并没有太多的功能。
This might not be ideal, but it means you don't have to move all logic for pagination, searching and filtering to client side.
这可能不太理想,但这意味着您不必将分页、搜索和过滤的所有逻辑移动到客户端。
TL;DR;
Override the Yii widgets which you already have functionality built for, and use them to output JSON rather than HTML.
TL,博士;覆盖您已经构建的Yii小部件,并使用它们输出JSON而不是HTML。
#3
2
In my experience, you can just access the existing controllers and actions (by cli or http) with your serverside JS. I did the same with my existing app that integrated with node.js.
根据我的经验,您可以使用您的服务器端JS访问现有的控制器和操作(通过cli或http)。我对与node.js集成的现有应用程序也做了同样的事情。
A benefit from this is that you can keep you exisitng code as a fallback for browsers not fully supporting your new implementations or for search bots who arent great with javascript in the first place.
这样做的好处是,你可以让你的代码成为浏览器的后备,因为浏览器不能完全支持你的新实现,或者你的搜索机器人根本不支持javascript。
I wrote the whole app in my models anyway, i mean data control etc, the actual functionality is in commands. And if the browser is not supported or disables javascript, everything falls back to the normal way with anchors and page loads, using the UrlManager and actual controllers and actions. An those controllers an action simple call the commands. Ofcourse with a helper converting the json output to usable data for the views.
我在我的模型中写了整个应用,我是说数据控制等等,实际的功能是在命令中。如果浏览器不受支持或禁用javascript,那么使用UrlManager和实际的控制器和操作,使用锚点和页面加载,一切都会回到正常的方式。这些控制器一个操作简单调用命令。当然,还有一个助手将json输出转换为视图的可用数据。
And the page load is with default php and controllers views anyway since it is already there.
页面加载使用默认的php和控制器视图,因为它已经在那里了。
#1
10
No, you should not move everything to client side, especially authentication and validation that could be bypassed otherwise.
不,您不应该将所有内容都转移到客户端,特别是在其他情况下可以绕过的身份验证和验证。
What you move to Emberjs is the yii-s View part of MVC, controller will output i.e JSON.
你移动到Emberjs的是MVC的yii-s视图部分,控制器会输出i。e JSON。
That Data then gets mapped to Embers Model part through Ember routing and controllers etc.
然后,这些数据通过余烬路由和控制器等被映射到余烬模型部分。
Since you are replacing Yii's presentation logic with ember you should not use Yii's UI classes like CGridView. Mixing them might be possible but that does not seem like a good idea. You have to run your own in Ember.
由于您正在替换Yii的表示逻辑,所以您不应该使用Yii的UI类,比如CGridView。混合它们可能是可能的,但这似乎不是一个好主意。你必须在烬里运行你自己的。
http://www.yiiframework.com/wiki/409/ember-js-with-yii-rest-backend-demo-application/
http://www.yiiframework.com/wiki/409/ember-js-with-yii-rest-backend-demo-application/
#2
7
Just to add a Yii perspective here. A lot of the "magic" of grid views/lists happens within the data provider (for complex searching, sorting and filtering) and by having the data formatted with parsed fields on models.
在这里添加一个Yii透视图。网格视图/列表的许多“魔力”发生在数据提供程序(用于复杂的搜索、排序和过滤)内,并通过模型上的解析字段对数据进行格式化。
So you could utilise the same concepts server side and just output the final JSON, paginated and all, from within your own widget; or even just override the grid view and output json rather than the view after all the data/configuration processing.
所以你可以使用相同的概念服务器端输出最终的JSON,分页等等,从你自己的小部件中;甚至只是覆盖网格视图并输出json,而不是所有数据/配置处理之后的视图。
Once you've got JSON down rather than HTML it's very easy to replicate the front end of the grid, there's really not much functionality going on there.
一旦你有了JSON而不是HTML,就很容易复制网格的前端,那里并没有太多的功能。
This might not be ideal, but it means you don't have to move all logic for pagination, searching and filtering to client side.
这可能不太理想,但这意味着您不必将分页、搜索和过滤的所有逻辑移动到客户端。
TL;DR;
Override the Yii widgets which you already have functionality built for, and use them to output JSON rather than HTML.
TL,博士;覆盖您已经构建的Yii小部件,并使用它们输出JSON而不是HTML。
#3
2
In my experience, you can just access the existing controllers and actions (by cli or http) with your serverside JS. I did the same with my existing app that integrated with node.js.
根据我的经验,您可以使用您的服务器端JS访问现有的控制器和操作(通过cli或http)。我对与node.js集成的现有应用程序也做了同样的事情。
A benefit from this is that you can keep you exisitng code as a fallback for browsers not fully supporting your new implementations or for search bots who arent great with javascript in the first place.
这样做的好处是,你可以让你的代码成为浏览器的后备,因为浏览器不能完全支持你的新实现,或者你的搜索机器人根本不支持javascript。
I wrote the whole app in my models anyway, i mean data control etc, the actual functionality is in commands. And if the browser is not supported or disables javascript, everything falls back to the normal way with anchors and page loads, using the UrlManager and actual controllers and actions. An those controllers an action simple call the commands. Ofcourse with a helper converting the json output to usable data for the views.
我在我的模型中写了整个应用,我是说数据控制等等,实际的功能是在命令中。如果浏览器不受支持或禁用javascript,那么使用UrlManager和实际的控制器和操作,使用锚点和页面加载,一切都会回到正常的方式。这些控制器一个操作简单调用命令。当然,还有一个助手将json输出转换为视图的可用数据。
And the page load is with default php and controllers views anyway since it is already there.
页面加载使用默认的php和控制器视图,因为它已经在那里了。