当View的方法被调用时,调用集合中的方法

发布于 2024-12-29 15:14:02 字数 2546 浏览 2 评论 0原文

我有一个这样的视图和集合:

window.DmnView = Backbone.View.extend({
    template: _.template($("#tmpl_dmnListItem").html()),
    events: {
        "click .getWhois": "showWhois",
        "click .getDomain": "toBasket"
    },
    initialize: function() {
        this.model.bind('change', this.render, this);
        this.model.bind('destroy', this.remove, this);
    },
    render: function() {
        return $(this.el)
                .attr("class", this.model.get("free") ? "dmnItem green" : this.model.get("checked") ? "dmnItem red" : "dmnItem red loader")
                .html(this.template(this.model.toJSON()));
    },
    remove: function() {
        $(this.el).remove();
    },
    showWhois: function() {
        showBoxes(this.model.get("info"));
        return false;
    },
    toBasket: function() {
        this.model.toBasket();
        console.log("view");
    }
});

window.DmnListApp = Backbone.View.extend({
    el: $("#regWrap"),
    events: {
        "keypress #dmnName": "checkAll"
    },
    initialize: function() {
        this.input = this.$("#dmnName");
        this.list = this.$("#dmnList");
        this.basket = this.$("#dmnBasket");
        dmnList.bind('add', this.addOne, this);
        dmnList.bind('all', this.render, this);
        DmnView.bind('toBasket', this.toBasket, this);
    },
    render: function() {

    },
    addOne: function(dmnItem) {
        var view = new DmnView({model : dmnItem});
        this.list.append(view.render());
    },
    checkOne: function(name, zone, price, days) {
        dmnList.create({name: name, zone: zone, price: price, days: days});
    },
    checkAll: function(e) {
        var name = this.input.val();
        if (!name || e.keyCode != 13) return;
        if (name == "")
            name = "yandex";
        dmnList.destroyAll();
        var zoneList = dmnList.domainsInfo.Name;
        var costList = dmnList.domainsInfo.CostOrder;
        var daysList = dmnList.domainsInfo.DaysToProlong;
        var parent = this;
        $.each(zoneList, function(key, zone) {
            parent.checkOne(name, zone, costList[key], daysList[key]);
        });
        this.input.val("");
    },
    toBasket: function(){
        console.log("collection");
    }
});

我希望在调用视图的方法 toBasket() 之后调用集合的方法 toBasket() 。为此,我在集合中执行以下操作:

DmnView.bind('toBasket', this.toBasket, this);

因此,如果这有效,我应该在我的 javascript 控制台中收到两条消息:

  1. 查看
  2. 集合

(也许以其他顺序)

但我只在控制台中看到“查看”消息。我做错了什么?

蒂亚!

I have a view and collection like this:

window.DmnView = Backbone.View.extend({
    template: _.template($("#tmpl_dmnListItem").html()),
    events: {
        "click .getWhois": "showWhois",
        "click .getDomain": "toBasket"
    },
    initialize: function() {
        this.model.bind('change', this.render, this);
        this.model.bind('destroy', this.remove, this);
    },
    render: function() {
        return $(this.el)
                .attr("class", this.model.get("free") ? "dmnItem green" : this.model.get("checked") ? "dmnItem red" : "dmnItem red loader")
                .html(this.template(this.model.toJSON()));
    },
    remove: function() {
        $(this.el).remove();
    },
    showWhois: function() {
        showBoxes(this.model.get("info"));
        return false;
    },
    toBasket: function() {
        this.model.toBasket();
        console.log("view");
    }
});

window.DmnListApp = Backbone.View.extend({
    el: $("#regWrap"),
    events: {
        "keypress #dmnName": "checkAll"
    },
    initialize: function() {
        this.input = this.$("#dmnName");
        this.list = this.$("#dmnList");
        this.basket = this.$("#dmnBasket");
        dmnList.bind('add', this.addOne, this);
        dmnList.bind('all', this.render, this);
        DmnView.bind('toBasket', this.toBasket, this);
    },
    render: function() {

    },
    addOne: function(dmnItem) {
        var view = new DmnView({model : dmnItem});
        this.list.append(view.render());
    },
    checkOne: function(name, zone, price, days) {
        dmnList.create({name: name, zone: zone, price: price, days: days});
    },
    checkAll: function(e) {
        var name = this.input.val();
        if (!name || e.keyCode != 13) return;
        if (name == "")
            name = "yandex";
        dmnList.destroyAll();
        var zoneList = dmnList.domainsInfo.Name;
        var costList = dmnList.domainsInfo.CostOrder;
        var daysList = dmnList.domainsInfo.DaysToProlong;
        var parent = this;
        $.each(zoneList, function(key, zone) {
            parent.checkOne(name, zone, costList[key], daysList[key]);
        });
        this.input.val("");
    },
    toBasket: function(){
        console.log("collection");
    }
});

I want Collection's method toBasket() to be called after View's method toBasket() was called. For this purpose I do the following in Collection:

DmnView.bind('toBasket', this.toBasket, this);

So, if this worked, I should receive two messages in my javascript console:

  1. view
  2. collection

(Maybe in other order)

But I only see "view" message in console. What I do wrong?

TIA!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2025-01-05 15:14:02

你快到了。在您的集合视图中,您尝试侦听 DmnView 事件 toBasket,但您的设置方式有点不正确。要监听事件,您必须绑定到要监听的特定实例,而不是类。因此,您需要将绑定从 initialize 移至 addOne,如下所示:

window.DmnListApp = Backbone.View.extend({
    // ...
    initialize: function() {
        this.input = this.$("#dmnName");
        this.list = this.$("#dmnList");
        this.basket = this.$("#dmnBasket");
        dmnList.bind('add', this.addOne, this);
        dmnList.bind('all', this.render, this);
        // Remove the DmnView bind here
    },
    addOne: function(dmnItem) {
        var view = new DmnView({model : dmnItem});
        // Bind to the DmnView instance here
        view.bind('toBasket', this.toBasket, this);
        this.list.append(view.render());
    },
    // ...
});

现在您的集合视图正在侦听事件 toBasket,您需要在 DmnView 视图中实际触发该事件。

在 Backbone 视图中,不会自动触发任何事件,因此您需要自己手动触发它,如下所示:

window.DmnView = Backbone.View.extend({
    // ...
    toBasket: function() {
        this.model.toBasket();
        console.log("view");

        // Trigger the event
        this.trigger('toBasket');
    }
});

您现在应该在控制台中看到两条消息。

You're almost there. In your collection view, you're attempting to listen to the DmnView event toBasket, but how you have it setup is a little incorrect. To listen to events, you have to bind to a specific instance you want to listen to, not a class. So you'll want to move the bind from initialize to addOne, like this:

window.DmnListApp = Backbone.View.extend({
    // ...
    initialize: function() {
        this.input = this.$("#dmnName");
        this.list = this.$("#dmnList");
        this.basket = this.$("#dmnBasket");
        dmnList.bind('add', this.addOne, this);
        dmnList.bind('all', this.render, this);
        // Remove the DmnView bind here
    },
    addOne: function(dmnItem) {
        var view = new DmnView({model : dmnItem});
        // Bind to the DmnView instance here
        view.bind('toBasket', this.toBasket, this);
        this.list.append(view.render());
    },
    // ...
});

Now that your collection view is listening for the event toBasket, you need to actually fire the event in your DmnView view.

In Backbone views, no events are automatically fired, so you'll need to manually trigger it yourself, like this:

window.DmnView = Backbone.View.extend({
    // ...
    toBasket: function() {
        this.model.toBasket();
        console.log("view");

        // Trigger the event
        this.trigger('toBasket');
    }
});

You should now see both messages in your console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文