将Ember.js与外部代码集成的最佳方法(例如,Android的WebView) [英] Best way to integrate Ember.js with external code (e.g., Android's WebView)

查看:130
本文介绍了将Ember.js与外部代码集成的最佳方法(例如,Android的WebView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在开发Ember.js应用程序时需要对外部代码做出反应的最佳实践是什么(例如,通过WebView从Android应用程序调用,或者其他代码段在范围之外)的Ember应用程序)。



在这种情况下,JavaScript世界和Java世界之间的公共接口是必需的。该接口可能是封装的对象,但它需要能够使Ember.js路由器作出反应来更改应用程序状态。 Ember.js中这种用例定义不是很好。例如,AngularJS似乎通过提供服务(可能是我的界面)和依赖注入来做一个更好的工作。但是,Ember.js提供哪些机制来构建App和外部世界之间的外部接口?



当然可以制作一些黑客(例如使用App。 容器),但我正在寻找一种符合框架作者愿景的方法。我的意思是...我不想选择一个框架,最后不要反对。这就是为什么这个具体的项目我正在考虑使用一个不那么有意义的框架,如Backbone.js。即使从Ember.js(非常非常小)的经验我也非常喜欢整体组织。

解决方案


我想知道最好的做法是什么...


我不确定有任何一个最佳实践与外部代码连接。


Android应用程序通过WebView ....在这种情况下有一个公共JavaScript世界和Java世界之间的界面是必需的。该接口可能是一个封装的对象,但它需要能够使Ember.js路由器反应更改应用程序状态


感。曾经为此可以使用控制器单身人士。像任何其他控制器一样,它将能够访问路由器。它也可能连接到其他控制器来修改其状态。

  App.ApplicationRoute = Ember.Route.extend({
setupController:function(){
bridge = this.controllerFor('bridge');
//这个工作但是使用全局常量
//依赖注入会更好,看以下...
ImaginaryJavaLibrary.sendStateChangesTo(bridge);
}
})

App.BridgeController = Ember.Controller.extend({
eventOne :function(data){
//对状态变化做出反应,也可以调用路由器
}
})




AngularJS似乎通过提供服务(可能是我的界面)和依赖注入来做得更好。但是,Ember.js提供哪些机制可以在App和外部世界之间建立一个外部接口。


服务看起来很酷。 FWIW我认为AngularJS在记录依赖注入方面做得更好,但肯定是Ember的核心部分。查看这篇文章,了解如何将对象(如您的界面)注入到ember应用程序中。



http://mcdowall.info/posts/ember-application-initializers/





另请参阅这篇文章,介绍如何与Pusher进行集成 - 我认为这与你想要做的很接近: / p>

http://livsey.org/blog/2013/02/10/integrating-pusher-with-ember/



FWIW这里是链接到我们讨论过的其他几种方法:




I'd like to know what is the best practice when developing an Ember.js app when it needs to react to external code (e.g.,called from and Android app through a WebView, or other pieces of code out of the scope of the Ember Application).

In such scenario having a public interface between the JavaScript world and the Java world is a necessity. That interface may be a an encapsulated object but it needs to be able to make Ember.js router react to change the application state. This kind of use case is not very well defined in Ember.js. For example, AngularJS seems to do a better job at this by providing services (that could be my interface) and Dependency injection. But what mechanisms does Ember.js offers to build an external interface between the App and the external world?

Of course that some hacks could be made (e.g., using App.container) but I'm looking for a way that is consonance with the vision of the frameworks authors. I mean... I don't wish to choose a framework and in the end keep fighting against it. That's why for this specific project I'm considering using a less opinionated framework like Backbone.js. Even though from the (very very small) experience I have with Ember.js I quite like the overall organization of if it.

解决方案

I'd like to know what is the best practice...

I'm not sure there is any one best-practice for connecting with external code.

Android app through a WebView....In such scenario having a public interface between the JavaScript world and the Java world is a necessity. That interface may be a an encapsulated object but it needs to be able to make Ember.js router react to change the application state

Makes sense. Once could use a controller singleton for this purpose. Like any other controller, it would be able to access the router. It might also connect to other controllers to modify their state.

App.ApplicationRoute = Ember.Route.extend({
  setupController: function() {
    bridge = this.controllerFor('bridge');
    // This "works" but uses a global constant
    // Dependency Injection would be better, see below...
    ImaginaryJavaLibrary.sendStateChangesTo(bridge);
  }
})

App.BridgeController = Ember.Controller.extend({
  eventOne: function(data) {
    //react to state change, maybe call router.
  }
})

AngularJS seems to do a better job at this by providing services (that could be my interface) and Dependency injection. But what mechanisms does Ember.js offers to build an external interface between the App and the external world

Services seem cool. FWIW I think maybe AngularJS does a better job at documenting Dependency Injection, but for sure it is a core part of ember as well. Check out this post for an explanation of how to inject objects (like your interface) into an ember application.

http://mcdowall.info/posts/ember-application-initializers/

http://emberjs.com/api/classes/Ember.Application.html#method_register

Also check out this post which describes how to integrate with Pusher - i think this is pretty close to what you'll want to do:

http://livsey.org/blog/2013/02/10/integrating-pusher-with-ember/

FWIW here are Links to a few other approaches we've talked about:

这篇关于将Ember.js与外部代码集成的最佳方法(例如,Android的WebView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆