清洁与Backbone.js的看法呢? [英] Cleaning views with backbone.js?

查看:87
本文介绍了清洁与Backbone.js的看法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Backbone.js的应用,并已经达到的地步,我有很多的路由器和观点重新presenting我的应用程序的每个部分。在下面的简化路由器的例子,我有两个位置; 帐户&安培; 用户

在每个位置都认为呈现其内容相互元素,名为 #appcontainer 。我常理说,我应该确保 删除 在绑定推出另一个prevent碰撞,DOM和诸如此类的东西之前,每个视图。

但我不知道自己是否已经被创建一个视图,我不能明确地调用 previousView.remove()无论是从我的路由器或视图内

难道是足以增加 $(this.el).empty()来每个视图的构造函数来清理任何可能previous绑定和元素从DOM?

下面是路由器的例子吗?

  VAR myRouter = Backbone.Router.extend({    路线:{
        账:账户,
        用户:用户
    },    账号:功能(){
        鉴于=新AccountView({});
        view.render();
    },    用户:函数(){
        鉴于=新UserView({});
        view.render();
    }});


解决方案

我有一个非常简单的实现,我刚开始我的应用程序现在,不知道这是怎么回事托起,从长远来看,但它看起来是这样的:

编辑:下面是整个文件会是什么样子。 this.render将myRouter的另一个功能。

  VAR myRouter = Backbone.Router.extend({
    路线:{
        路径/要/帐户':'账户',
        路径/要/用户':'用户'
    }    账号:功能(){
        鉴于=新AccountView({});
        this.render(视图);
    },    用户:函数(){
        鉴于=新UserView({});
        this.render(视图);
    },    渲染:功能(视图){
        //关闭当前视图
        如果(this.currentView){
            this.currentView.remove();
        }        //呈现新的视图
        view.render();        //设置当前视图
        this.currentView =视图。        返回此;
    }
});

I am working on a backbone.js-application and have reached the point where I have a number of routers and views representing each part of my application. In the simplified router example below, I have two locations; account & users.

Both view in each location render their content to a mutual element, named #appcontainer. My common sense says that I should make sure to remove each view before launching another to prevent collisions in bindings, DOM and whatnot.

But as I cannot know for sure whether a view already has been created, I cannot explicitly call previousView.remove() either from inside my router or views.

Would it be sufficient to add $(this.el).empty() to the constructor of each view to clean out any eventual previous bindings and elements from the DOM?

Here's the router example?

var myRouter = Backbone.Router.extend({

    routes: {
        "account": "account",
        "users": "users"
    },

    account: function() {
        view = new AccountView({});
        view.render();
    },

    users: function() {
        view = new UserView({});
        view.render();
    }

});

解决方案

I have a really simple implementation, I'm just starting my application now and don't know how this is going to hold up in the long run, but it looks something like this:

Edit: Here's what the entire file would look like. this.render will be another function of myRouter.

var myRouter = Backbone.Router.extend({
    routes: {
        'path/to/account' : 'account',
        'path/to/users': 'users'
    }

    account: function() {
        view = new AccountView({});
        this.render(view);
    },

    users: function() {
        view = new UserView({});
        this.render(view);
    },

    render: function (view) {
        //Close the current view
        if (this.currentView) {
            this.currentView.remove();
        }

        //render the new view
        view.render();

        //Set the current view
        this.currentView = view;

        return this;
    }
});

这篇关于清洁与Backbone.js的看法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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