如何*不*在退出Ember.js中的路线时销毁View

Pan*_*agi 13 ember.js ember-old-router

关于新的Ember.js路由系统(在此描述),如果我理解正确,当您退出路由时视图将被销毁.

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


更新:看起来,除非在新路线中更换插座视图,否则不会销毁视图.例如,如果您在某个{{outlet master}}中使用ViewA的stateA,并且您使用{{outlet master}}中的ViewB转到stateB,则ViewB将取代ViewA.解决这个问题的方法是在需要保留视图时定义多个出口,例如{{outlet master1}},{{outlet master2}},...

一个很好的功能是能够将一组视图传递到插座.并且还可以选择在退出路线时是否会销毁视图或隐藏视图.

Pan*_*agi 9

我已经弄清楚如何修改路由系统,以便插入插座的视图不会被破坏.首先,我重写Handlebars outlet帮助器,以便它加载Ember.OutletView{{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);
});
Run Code Online (Sandbox Code Playgroud)

其中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')

});
Run Code Online (Sandbox Code Playgroud)

基本上我们覆盖_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;
    }
});
Run Code Online (Sandbox Code Playgroud)