最好的方式来命名多个或适合emberjs应用程序 [英] Best way to namespace multiple or suit of emberjs apps

查看:56
本文介绍了最好的方式来命名多个或适合emberjs应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的问题仍然在emberjs命名空间上,但这次在一套多个emberjs的上下文中应用程序将包含在多个rails-engine中,以便每个emberjs应用程序都是具有自己的控制器,模型,视图和路由器的独立应用程序。但是,它们仍然需要共享ember数据关联。这些轨道引擎将被包含在主轨应用程序中,每个引擎都代表了应用程序的主要功能。



在这个 jsfiddle ,我想出了3个方法来命名空间,但我想知道哪一个是emberjs的方式:

  **方法1 ** 





  MainRailsApp = Ember.Application.create (); 

RailsEngine = Ember.Namespace.create();

RailsEngine2 = Ember.Namespace.create();

MainRailsApp.Store = DS.Store.extend(); **继承自Ember.Application **
MainRailsApp.Router = Em.Router.extend()**继承自Ember.Application **

console.log(RailsEngine.toString() ); // RailsEngine
console.log(RailsEngine2.toString()); // RailsEngine2


RailsEngine.Model = DS.Model.extend
RailsEngine2.model = DS.Model.extend

该模型的共享关联虽然从不同的命名空间继承

  Contact.Model = RailsEngine.Model.extend({
address:DS.attr('string'),
user:DS.belongsTo('User.Model')
});

User.Model = RailsEngine2.Model.extend({
name:DS.attr('string'),
contacts:DS.hasMany('Contact.Model') ,

});



**方法2 **

//所有不同的emberjs应用程序共享一个命名空间,但不同的实例

  Yp = Ember.Namespace 。延伸(); 

UserRailsEngine = Yp.create();

ContactRailsEngine = Yp.create();

PaymentRailsEngine = Yp.create();

Yp.Jk = Ember.Application.extend();

Yp.Jk.create();
Yp.Router = Em.Router.extend(); **继承自Ember.Namespace **
Yp.Store = DS.Store.extend({}); **继承自Ember.Namespace **


console.log(UserRailsEngine.toString()); // UserRailsEngine

console.log(PaymentRailsEngine.toString()); // PaymentRailsEngine

UserRailsEngine.Model = DS.Model.extend
ContactRailsEngine.Model = DS.Model.extend

这个模型可以共享关联,它们有一个命名空间但不同的实例

  Contact.Model = ContactRailsEngine.Model .extend({
address:DS.attr('string'),
user:DS.belongsTo('User.Model')
});

User.Model = UserRailsEngine.Modelextend({
name:DS.attr('string'),
contacts:DS.hasMany('Contact.Model')
});


**方法3 **

//一个命名空间,但每个emberjs应用程序的命名空间的多个子类

  Mynamespace = Ember.Namespace.extend ); 

Order = Mynamespace.extend();

OrderRailsEngine = Order.create();

Event = Mynamespace.extend();

EventRailsEngine = Event.create();

console.log(OrderRailsEngine.toString()); // OrderRailsEngine

console.log(EventRailsEngine.toString()); // EventRailsEngine


**其他问题**

1。我仍然可以在所有3种方法中使用hasMany和belongsTo关联ember数据模型。


  1. 我还不知道如何路由器将被处理。你认为命名空间应该在主应用程序和每个rails-engine中,以便它们仍然可以无缝地工作。


  2. 你的建议是什么如何处理ember数据DS.Store命名空间,因为每个Ember数据模型将命名为每个引擎,我仍然希望ember数据DS.Store识别并使用引擎中包含的不同emberjs模型。 p>


  3. Ember.Namespace自动初始化,就像Ember.Application被自动初始化一样。



非常感谢您的时间。

解决方案

一个修改后的准备2似乎是emberjs的方式。所有不同的emberjs应用程序仍将共享一个命名空间,而不是该命名空间的不同实例,它们将共享一个实例。最终 jsfiddle



从下面的代码中可以看到这种方法,它从以下链接中提取:



https://github.com/emberjs/data/blob/master/packages/ember-data /tests/integration/embedded/embedded_dirtying_test.js

  var attr = DS.attr; 
var发布,评论,用户,投票,博客;
var适配器,应用程序;
var adapter,store,post;
var forEach = Ember.EnumerableUtils.forEach;

App = Ember.Namespace.create({name:App});

User = App.User = DS.Model.extend({
name:attr('string')
});

Vote = App.Vote = DS.Model.extend({
voter:attr('string')
});

评论= App.Comment = DS.Model.extend({
title:attr('string'),
user:DS.belongsTo(User),
票:DS.hasMany(投票)
});

Blog = App.Blog = DS.Model.extend({
title:attr('string')
});

Post = App.Post = DS.Model.extend({
title:attr('string'),
comments:DS.hasMany(Comment),
博客:DS.belongsTo(博客)
});

适配器= DS.RESTAdapter.extend();

Adapter.map(评论,{
用户:{embedded:'always'},
投票:{embedded:'always'}
});

Adapter.map(Post,{
comments:{embedded:'always'},
blog:{embedded:'load'}
});

adapter = Adapter.create();

store = DS.Store.create({
适配器:适配器
});

所以在比赛中使用它在轨道引擎上变成:

  // App = Ember.Namespace.create(); 
App = Ember.Namespace.create({name:App});
Main = App.MainApp = Ember.Application.extend();
Main.create();

UserEngine = App.UserEngine = DS.Model.extend({
name:DS.attr('string')
});

VoteEngine = App.VoteEngine = DS.Model.extend({
voter:DS.attr('string')
});

console.log(Main.toString()); //App.MainApp
console.log(UserEngine.toString()); //App.UserEngine
console.log(VoteEngine.toString()); //App.VoteEngine


First a Merry Xmas to you and thanks for helping with suggestions.

My question is still on emberjs namespace but this time in the context of a suite of multiple emberjs apps which will be contained within multiple rails-engine, so that each emberjs app is a standalone app with its own controllers, models, views and routers. However, they will still need to share ember-data associations. These rails-engines will inturn be included in the main-rails app where each engine represents a major feature of the app.

In this jsfiddle, I came up with 3 approach to namespace, but I will like to know which one is the emberjs way:

                **Approach 1** 

//Each emberjs app with its own namespace

MainRailsApp = Ember.Application.create();

RailsEngine = Ember.Namespace.create();

RailsEngine2 = Ember.Namespace.create();

MainRailsApp.Store= DS.Store.extend();    **inherits from Ember.Application**
MainRailsApp.Router = Em.Router.extend()  **inherits from Ember.Application**

console.log(RailsEngine.toString());       //RailsEngine
console.log(RailsEngine2.toString());      //RailsEngine2


RailsEngine.Model = DS.Model.extend
RailsEngine2.model = DS.Model.extend

Can this model's share association though they inherit from different namespace

Contact.Model = RailsEngine.Model.extend({
       address:   DS.attr('string'),
       user:      DS.belongsTo('User.Model')      
});

User.Model = RailsEngine2.Model.extend({
      name: DS.attr('string'),
      contacts: DS.hasMany('Contact.Model'),

});



                 **Approach 2**

//All the different emberjs apps share one namespace but different instances

Yp = Ember.Namespace.extend();

UserRailsEngine = Yp.create();

ContactRailsEngine = Yp.create();

PaymentRailsEngine = Yp.create();

Yp.Jk = Ember.Application.extend();

Yp.Jk.create();
Yp.Router = Em.Router.extend();      **inherits from the Ember.Namespace**
Yp.Store = DS.Store.extend({ });     **inherits from the Ember.Namespace**


console.log(UserRailsEngine.toString());         //UserRailsEngine

console.log(PaymentRailsEngine.toString());      //PaymentRailsEngine

UserRailsEngine.Model = DS.Model.extend
ContactRailsEngine.Model = DS.Model.extend

Can this models share association, they have one namespace but different instance

Contact.Model = ContactRailsEngine.Model .extend({
       address:   DS.attr('string'),
       user:      DS.belongsTo('User.Model')      
});

User.Model = UserRailsEngine.Modelextend({
      name: DS.attr('string'),
      contacts: DS.hasMany('Contact.Model')    
});


            **Approach 3**

//One namespace but multiple subclasses of the namespace for each emberjs app

Mynamespace = Ember.Namespace.extend();

Order = Mynamespace.extend();

OrderRailsEngine = Order.create();

Event = Mynamespace.extend();

EventRailsEngine = Event.create();

console.log(OrderRailsEngine.toString());           //OrderRailsEngine

console.log(EventRailsEngine.toString());           //EventRailsEngine


**Additional questions**

1. Can I still associate ember-data models using hasMany and belongsTo in all of the 3 approach.

  1. I am still not sure how the router will be handled. What do you think the namespace should be in the main-app and each of the rails-engine, so that they still work seamlessly.

  2. What your suggestion on how to handle ember-data DS.Store namespacing since each ember-data model will be namespaced to each engine and I still want the ember-data DS.Store to recognize and work with the the different emberjs models contained in the engines.

  3. Are Ember.Namespace auto-initialized just like Ember.Application is auto-initialized.

  4. Alternative patterns are welcome.

Many thanks for your time.

解决方案

A modified approah 2 seems to be the emberjs way to go. All the different emberjs apps will still share one namespace but instead of different instances of that namespace, they will share one instance. Final jsfiddle

This approach can be seen from the code pasted below, which was extracted from the link below:

https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/embedded/embedded_dirtying_test.js

var attr = DS.attr;
var Post, Comment, User, Vote, Blog;
var Adapter, App;
var adapter, store, post;
var forEach = Ember.EnumerableUtils.forEach;

App = Ember.Namespace.create({ name: "App" });

User = App.User = DS.Model.extend({
  name: attr('string')
});

Vote = App.Vote = DS.Model.extend({
  voter: attr('string')
});

Comment = App.Comment = DS.Model.extend({
  title: attr('string'),
  user: DS.belongsTo(User),
  votes: DS.hasMany(Vote)
});

Blog = App.Blog = DS.Model.extend({
  title: attr('string')
});

Post = App.Post = DS.Model.extend({
  title: attr('string'),
  comments: DS.hasMany(Comment),
  blog: DS.belongsTo(Blog)
});

Adapter = DS.RESTAdapter.extend();

Adapter.map(Comment, {
  user: { embedded: 'always' },
  votes: { embedded: 'always' }
});

Adapter.map(Post, {
  comments: { embedded: 'always' },
  blog: { embedded: 'load' }
});

adapter = Adapter.create();

store = DS.Store.create({
  adapter: adapter
});

So in the contest of using it across rails engines it becomes:

   //App = Ember.Namespace.create();
 App = Ember.Namespace.create({ name: "App" });
 Main = App.MainApp = Ember.Application.extend();
 Main.create();

 UserEngine = App.UserEngine = DS.Model.extend({
    name: DS.attr('string')
 });

 VoteEngine = App.VoteEngine = DS.Model.extend({
   voter: DS.attr('string')
 });

 console.log(Main.toString());                       //App.MainApp
 console.log(UserEngine.toString());                 //App.UserEngine 
 console.log(VoteEngine.toString());                 //App.VoteEngine 

这篇关于最好的方式来命名多个或适合emberjs应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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