Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

两层以上的ChildAction里发生global redirect的时候会报错 #152

Closed
chestnutchen opened this issue Jan 19, 2015 · 6 comments
Closed

Comments

@chestnutchen
Copy link
Member

hijack里边会判断是不是子childAction已经做过处理了,但是其实如果是global redirect的话,肯定是处理过的。所以可以加上判断是否是global redirect,是就不用去检查子childAction是否处理过。

    function isChildActionRedirected(e) {
        // 除低版本IE外,其它浏览器是可以在事件对象上加自定义属性的,IE每次都生成新的事件对象所以保留不了这些属性,
        // 在这里优先用自定义属性控制,避免对DOM树无意义的遍历,只有在没有属性的时候,才向后兼容至DOM树的遍历
        if (e.isChildActionRedirected) {
            return true;
        }

        var innermostContainer = e.target || e.srcElement;
        while (innermostContainer) {
            // 是Action容器的元素肯定符合以下条件:
            //
            // - 有个`id`,因为没有`id`不能渲染子Action
            // - 这个`id`在`childActionMapping`里是有对应的值的
            if (innermostContainer.id && currentController.childActionMapping[innermostContainer.id]) {
                break;
            }

            innermostContainer = innermostContainer.parentNode;
        }
        // 如果最接近被点击的链接的Action容器是不是当前的这个容器,就说明在当前容器和链接之间还有一层以上的子Action,
        // 那么这个子Action肯定会处理掉这个链接的跳转,不需要这里处理了
        if (innermostContainer.id !== actionContext.container) {
            e.isChildActionRedirected = true;
            return true;
        }

        return false;
    }

报错的原因是,childActionglobal redirect的时候会同步把自己的容器销毁,然后再上层的Action在第二次hijack的时候又检查isChildActionRedirected。虽然容器在内存中的片段还可以访问,但是到innermostContainer是该容器的时候,innermostContainer.parentNode就是null了,所以if (innermostContainer.id !== actionContext.container) {就报错了

还有个小问题就是hijack里边的

    var redirectAttributes = (target.getAttribute('data-redirect') || '').split(/[,\s]/);
    var redirectOptions = {};
    for (var i = 0; i < redirectAttributes.length; i++) {
        var redirectAttributeName = util.trim(redirectAttributes[i]);
        redirectOptions[redirectAttributeName] = true;
    }

会产生一个对象{"": true}

解决方案可以是innermostContainer && innermostContainer.id !== actionContext.container或者提前判断global redirect,感觉提前判断global redirect会好一点

@chestnutchen chestnutchen changed the title 两层ChildAction里发生global redirect的时候会报错 两层以上的ChildAction里发生global redirect的时候会报错 Jan 19, 2015
@otakustay
Copy link
Member

Fix in #152

@Justineo
Copy link
Member

好像还有点问题,看逻辑 data-redirect 是一个 , 分隔的串,不能直接起名 scope,而且直接比较 === 'scope' 不太妥。@chestnutchen 再改下吧~

@chestnutchen
Copy link
Member Author

昨晚改成了redirectType还没提交,再看看data-redirect的全集都有哪些

@otakustay
Copy link
Member

全集在meta/RedirectOption里有写

Best regards

Gray Zhang

在 2015年1月20日 上午10:58:50, chestnut (notifications@github.com) 写到:

昨晚改成了redirectType还没提交,再看看data-redirect的全集都有哪些


Reply to this email directly or view it on GitHub.

@Justineo
Copy link
Member

@otakustay meta 里面好像漏了 global,还有这里是不是多传了一个 options 参数?

@otakustay
Copy link
Member

是的,在PR中一并给我吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants