13

关于新的 Ember.js 路由系统(在此处描述),如果我理解正确,退出路由时视图会被破坏。

有没有办法在退出路线时绕过视图的破坏,以便在用户重新进入路线时保留视图的状态?


更新:看起来,除非在新路线中替换出口视图,否则视图不会被破坏。例如,如果您在某个 {{outlet master}} 中的 ViewA 处于 stateA 并且您在 {{outlet master}} 中使用 ViewB 进入 stateB,那么 ViewB 将替换 ViewA。解决此问题的一种方法是在需要保留视图时定义多个出口,例如 {{outlet master1}}、{{outlet master2}}、...

一个不错的功能是能够将一系列视图传递给出口。并且还可以在退出路线时选择视图是被破坏还是被隐藏。

4

2 回答 2

9

我已经弄清楚如何修改路由系统,以便插入到插座中的视图不会被破坏。首先,我重写 Handlebarsoutlet助手,以便它加载Ember.OutletViewinto {{outlet}}

Ember.Handlebars.registerHelper('outlet', function(property, options) {
  if (property && property.data && property.data.isRenderData) {
    options = property;
    property = 'view';
  }

  options.hash.currentViewBinding = "controller." + property;

  return Ember.Handlebars.helpers.view.call(this, Ember.OutletView, options);
});

其中Ember.OutletView扩展Ember.ContainerView如下:

Ember.OutletView = Ember.ContainerView.extend({
    childViews: [],

    _currentViewWillChange: Ember.beforeObserver( function() {
        var childViews = this.get('childViews');

            // Instead of removing currentView, just hide all childViews
            childViews.setEach('isVisible', false);

    }, 'currentView'),

    _currentViewDidChange: Ember.observer( function() {
        var childViews = this.get('childViews'),
            currentView = this.get('currentView');

        if (currentView) {
            // Check if currentView is already within childViews array
            // TODO: test
            var alreadyPresent = childViews.find( function(child) {
               if (Ember.View.isEqual(currentView, child, [])) {          
                   return true;
               } 
            });

            if (!!alreadyPresent) {
                alreadyPresent.set('isVisible', true);
            } else {
                childViews.pushObject(currentView);
            }
        }
    }, 'currentView')

});

基本上我们覆盖_currentViewWillChange()并只是隐藏所有childViews而不是删除currentView. 然后_currentViewDidChange()我们检查是否currentView已经在里面childViews并采取相应的行动。这Ember.View.isEqualUnderscoreisEqual的修改版本:

Ember.View.reopenClass({ 
    isEqual: function(a, b, stack) {
        // Identical objects are equal. `0 === -0`, but they aren't identical.
        // See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal.
        if (a === b) return a !== 0 || 1 / a == 1 / b;
        // A strict comparison is necessary because `null == undefined`.
        if (a == null || b == null) return a === b;
        // Unwrap any wrapped objects.
        if (a._chain) a = a._wrapped;
        if (b._chain) b = b._wrapped;
        // Compare `[[Class]]` names.
        var className = toString.call(a);
        if (className != toString.call(b)) return false;

        if (typeof a != 'object' || typeof b != 'object') return false;
        // Assume equality for cyclic structures. The algorithm for detecting cyclic
        // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
        var length = stack.length;
        while (length--) {
            // Linear search. Performance is inversely proportional to the number of
            // unique nested structures.
            if (stack[length] == a) return true;
        }
        // Add the first object to the stack of traversed objects.
        stack.push(a);
        var size = 0, result = true;
        // Recursively compare objects and arrays.
        if (className == '[object Array]') {
            // Compare array lengths to determine if a deep comparison is necessary.
            size = a.length;
            result = size == b.length;
            if (result) {
                // Deep compare the contents, ignoring non-numeric properties.
                while (size--) {
                    // Ensure commutative equality for sparse arrays.
                    if (!(result = size in a == size in b && this.isEqual(a[size], b[size], stack))) break;
                }
            }
        } else {
            // Objects with different constructors are not equivalent.
            if (a.get('constructor').toString() != b.get('constructor').toString()) {
                return false;
            }

            // Deep compare objects.
            for (var key in a) {
                if (a.hasOwnProperty(key)) {
                    // Count the expected number of properties.
                    size++;
                    // Deep compare each member.
                    if ( !(result = b.hasOwnProperty(key) )) break;
                }
            }
        }
        // Remove the first object from the stack of traversed objects.
        stack.pop();
        return result;
    }
});
于 2012-07-17T07:42:21.453 回答
4

这样当用户重新进入路线时,视图的状态就会被保留。

相反,我会将该信息存储在控制器(或状态管理器)中,以便在重新进入路由时,使用旧状态初始化新视图。那有意义吗?因此,例如,如果它是一个帖子列表,并且其中一个被选中,您可以将关于哪个帖子被选中的数据存储在控制器(或状态管理器)中。在访问特定帖子然后返回列表后,将选择相同的帖子。

我可以想象一个用例,这不会很有用(例如滚动到长列表中的特定位置)所以也许这不能回答你的问题。

于 2012-06-18T17:07:17.640 回答