控制器策略/垃圾收集(销毁) [英] Controller Strategy / Garbage Collection (destroy)

查看:89
本文介绍了控制器策略/垃圾收集(销毁)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找出我的应用程序的ember最佳实践,关于MVC。也可供参考,我正在使用ember-data,ember-layout和ember-route-manager。



我将以User为例:



我想要做的是获取来自数据库的用户模型...然后将其包装在UserController中,并将模型设置为content属性...然后在View中,我想绑定到控制器功能,以及针对模型级数据的 controller.content 。所以控制器可能看起来像:

  App.UserViewController = Em.Object.create({
content:userRecord ,
isFollowingBinding:'content.you_follow',
toggleFollow:function(){
使服务器调用更改以下标志
}
});

然后视图可以绑定到 {{controller.content.name}} {{#if controller.isFollowing}} {{actiontoggleFollowingtarget =controller}}



,但是说我从数据库中获取用户模型的列表...我觉得应该发生的是每个模型都应该用控制器包装, 应该作为列表返回...所以该视图将有一个UserController的列表



顺便提一句,我已经做了这个..并且它工作得很好....除了每当我重新加载列表,我用新的控制器包装所有新的模型对象...随着时间的推移,内存中的控制器数量越来越大。在我的基础控制器类,我正在记录调用摧毁,我不会看到它有发生



当涉及到Em.View ...我知道每次从屏幕上删除它时,.destroy()都会得到调用(我也记录它们)。所以如果我把我的代码移动到一个视图,我知道它会被毁坏和重建每一次...但我不觉得像toggleFollow()应该是在视图中的功能...



这些问题:




  • MVC是如何工作的?每个模型的实例包裹在该模型的控制器中?哪里可以为一个屏幕创建大量的控制器实例?

  • 如果我按照这种方法,那么我负责destroy()我创建的所有控制器?或者是我上面描述的功能真的意味着一个View,而Ember会创建/销毁它们,因为它们被添加/从屏幕上删除?还允许模板设计者决定他们需要什么功能(如果他们只需要 {{user.name}} ,那么无需实例化其他控制器/视图类...但如果他们需要切换按钮,那么他们可以在 {{#查看App.UserViewController contentBinding =this}}

$ b中包含该模板的一部分
$ b

我重写了几次...希望这是有道理的。

解决方案

我不会将每个用户包装到自己的控制器中。



而是将用户绑定到一个视图,例如 App.UserView ,并处理该视图上的 toggleFollow 。然后,此操作将将其操作委托给将处理服务器调用的控制器,请参阅 http://jsfiddle.net/ pangratz666 / hSwEZ /



Handlebars

 < script type =text / x-handlebars> 
{{#each App.usersController}}
{{#view App.UserView userBinding =thiscontrollerBinding =App.usersController}}
{{user.name}}
{{#if isFollowing}}
< a {{actiontoggle以下}} class =clickable> stop following< / a>
{{else}}
< a {{actiontoggle以下}} class =clickable>开始跟随< / a>
{{/ if}}
{{#if user.isSaving}}保存... {{/ if}}
{{/ view}}
{{/每个}}
< / script>

JavaScript

  App.usersController = Ember.ArrayProxy.create({
content: ],

toggleFollowing:function(user){
user.set('isSaving',true);
Ember.run.later(function(){
user.toggleProperty('you_follow');
user.set('isSaving',false);
},1000);
}
});

App.UserView = Ember.View.extend({
isFollowingBinding:'user.you_follow',
toggleFollowing:function(){
var user = this。 get('user');
var controller = this.get('controller');
controller.toggleFollowing(user);
}
});


Trying to figure out the "ember best practices" for my app, regarding MVC. also for reference, I'm using ember-data, ember-layout, and ember-route-manager.

I'll use User as an example:

what I feel like I want to do is to get a User model from the database... then wrap it in a UserController, and set the model on a "content" property... then in a View, I want to bind to the controller for some functionality, and to the controller.content for model-level data. so a controller might look something like:

App.UserViewController = Em.Object.create({   
    content: userRecord,
    isFollowingBinding : 'content.you_follow', 
    toggleFollow: function() { 
       make server call to change following flag 
    }
});

then the view could bind to the {{controller.content.name}}, or {{#if controller.isFollowing}}, or {{action "toggleFollowing" target="controller"}}

but say I get a list of User models back from the database... I feel like what should happen is that each of those models should be wrapped with a controller, and that should be returned as a list... so the view would have a list of UserControllers

Incidentally, I've done this... and it is working nicely.... except that everytime I reload the list, I wrap all of the new model objects with new controllers... and over time, the # of controllers in memory get larger and larger. on my base Controller class, I'm logging calls to "destroy", and I dont see it ever happening

when it comes to Em.View... I know that everytime it is removed from the screen, .destroy() gets calls (I am logging those as well). so if I were to move my code into a view, i know it will get destroyed and recreated everytime... but I dont feel like the functionality like toggleFollow() is supposed to be in view...

SO QUESTIONS:

  • is this how MVC is supposed to work? every instance of a model wrapped in a controller for that model? where there could be lots of controller instances created for one screen?
  • if I go down this approach, then I'm responsible for destroy()ing all of the controllers I create?
  • or is the functionality I've described above really meant for a View, and them Ember would create/destroy them as they are added/removed from the screen? also allowing template designers to decide what functionality they need (if they just need the {{user.name}}, theres no need to instantiate other controller/view classes... but if they need a "toggle" button, then they could wrap that part of the template in {{#view App.UserViewController contentBinding="this"}} )

I re-wrote this a few times... hopefully it makes sense....

解决方案

I wouldn't wrap every user into an own controller.

Instead I would bind the user to a view, say App.UserView and handle the action toggleFollow on that view. This action will then delegate it's action to a controller which will handle the server call, see http://jsfiddle.net/pangratz666/hSwEZ/

Handlebars:

<script type="text/x-handlebars" >
    {{#each App.usersController}}
        {{#view App.UserView userBinding="this" controllerBinding="App.usersController"}}            
            {{user.name}}
            {{#if isFollowing}}
                <a {{action "toggleFollowing"}} class="clickable" >stop following</a>
            {{else}}
                <a {{action "toggleFollowing"}} class="clickable" >start following</a>
            {{/if}}
            {{#if user.isSaving}}saving ...{{/if}}
        {{/view}}
    {{/each}}
</script>​

JavaScript:

App.usersController = Ember.ArrayProxy.create({
    content: [],

    toggleFollowing: function(user) {
        user.set('isSaving', true);
        Ember.run.later(function() {
            user.toggleProperty('you_follow');
            user.set('isSaving', false);
        }, 1000);
    }
});

App.UserView = Ember.View.extend({
    isFollowingBinding: 'user.you_follow',
    toggleFollowing: function() {
        var user = this.get('user');
        var controller = this.get('controller');
        controller.toggleFollowing(user);
    }
});
​

这篇关于控制器策略/垃圾收集(销毁)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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