如何*不退出在Ember.js路由时*摧毁查看(How *not* to destroy View

2019-07-30 00:17发布

关于新Ember.js路由系统(形容这里 ),如果我理解正确的意见,当你退出的路线被破坏。

是否有任何方式在离开的路线绕过的观点破坏,从而使该视图的状态保存,当用户再次进入路线?


更新:看起来,除非出口观点在新的路线被替换的意见不被破坏。 对于例如,如果你在状态σ与ViewA在一些{{出口主}},你去与ViewB到stateB {{出口主}},然后ViewB将取代ViewA。 解决的办法是当你需要保留的观点来定义多个网点,例如,{{出口master1}},{{出口master2}},...

一个很好的功能。将意见数组传递到出口的能力。 而且还可以选择是否意见将被销毁,或只是被隐藏,在退出的路径。

Answer 1:

因为我已经弄明白如何修改路由系统,使插入插座的观点不被破坏。 首先我重写车把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);
});

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.isEqual是修改后的版本下划线isEqual

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;
    }
});


Answer 2:

因此,当用户重新进入路线图的状态被保留。

我想,而是存储在控制器中的信息(或状态管理器),以便当路由重新进入,新的视图与老态初始化。 那有意义吗? 因此,举例来说,如果它的职位名单,以及一个被选中,你将存储哪些职位是在控制器(或状态管理器)中选择的数据。 访问特定的帖子,然后回来列表后,同样的职位将被选中。

我可以想像,不回答你的问题的使用情况,这些都不会是非常有用的(例如,滚动到特定位置在一个长长的清单),所以也许。



文章来源: How *not* to destroy View when exiting a route in Ember.js