MarionetteJS:使用集合与不同的布局两个或两个以上的意见 [英] MarionetteJS: Using a collection with two or more views for different layouts

查看:142
本文介绍了MarionetteJS:使用集合与不同的布局两个或两个以上的意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,为了简便起见,你能想到的一个文件夹树,您可以选择使用复选框多个文件夹的UI控制器。选择会保存在模型和拯救有罕见的也有一些操作触发。

I have a UI controller that for simplicity you can think as a Folder Tree where you can select multiple folders with checkboxes. The selection is saved in the model and "on save" there rare also a few actions triggered.

有关可用性的原因,我们要使用同一控制器的两个不同的可视化效果。两个用户界面控制器可以在一些布局是同时present,在别人只有一个的时间。

For usability reasons, we want to use two different visualizations of the same controller. The two UI controllers can in some layouts be present at the same time, in others only one at the time.

这将是很好能够跨越2 UI控制器重复使用相同的集合实例。

It would be nice to be able to reuse the same collection instance across the 2 UI controllers.

我可以在一个模块pretty做到这一点很容易,但我应该怎么构建的code,使其在不同模块的工作?我在想有高配车型和集合类的模块,一个模块与视图1和另一个与视图2的,但如果是把收集的实例,使之与世界其他地方沟通的最佳场所。

I can do this in one module pretty easily but how should I structure the code to make it work across different modules? I was thinking of having a module with models and collection classes, one module with View1 and another with View2, but where is the best place to put the instance of the collection and make it communicate with the rest of the world.

什么是分享骨干型号/集合实例的最佳实践与木偶两个或两个以上的看法?

What are the best practices to share Backbone Models/Collections instances with two or more Views in Marionette?

推荐答案

一种方法是在一个单独的模块的实体(模块/收藏品),和你的各种模块要求他们。

One approach is to have your entities (modules/collections) in a separate module, and have your various modules request them.

一个实体模块(含集合定义和请求处理)的例子:<一href=\"https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/contact.js#L89\" rel=\"nofollow\">https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/contact.js#L89

Example of an entity module (with collection definition and request handler): https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/contact.js#L89

请求集合(7号线)的例子:<一href=\"https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L7\" rel=\"nofollow\">https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L7

Example of requesting a collection (line 7): https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L7

在您的情况下,同一个实例可以重复使用,如果需要的话。但要确保你已经制定某种机制来合理地显示最新数据(即获取服务器上的收集,以获得最最新数据)。

In your case, the same instance can be reused, if desired. But make sure you have some mechanism in place to display reasonably fresh data (i.e. fetching the collection on the server to get the most up to date data).

您可以使用JavaScript的关闭机制,这样做的,例如:

You can use javascript's closure mechanism to do so, e.g.:

ContactManager.module('Entities', function(...){
  var contacts = new Entities.ContactCollection(...);
  contacts.fetch();

  ContactManager.reqres.setHandler("contact:entities", function(){
    return contacts;
  });

  ContactManager.commands.setHandler("contact:entities:update", function(){
    return contacts.fetch();
  });
});

然后,在你的应用程序,你会使用 ContactManager.request(联系人:实体)来获得联系人和的ContactManager。执行(联系人:实体:更新)

的请求和命令的区别在于bascially语义:从应用程序的另一部分请求数据,对订货一些工作要做

The difference between a request and command is bascially semantic: requesting data from another part of the application, versus ordering some work to be done.

使用请求 - 回应使您的应用程序设计更好的(松散耦合,封装)。数据附加到 App.SomeNamespace.mycollection 也将工作(我在某些情况下完成的),但它会导致紧耦合,打破封装和我不会' ŧ推荐它对于大型应用程序。

Using request-responses allows your application to be designed better (loose coupling, encapsulation). Attaching the data to App.SomeNamespace.mycollection will also work (I've done it in certain cases), but it leads to tight coupling, breaks encapsulation, and I wouldn't recommend it for large applications.

这篇关于MarionetteJS:使用集合与不同的布局两个或两个以上的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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