Collection.get返回的值在ribs.js中未定义 [英] Collection.get returning undefined in backbone.js

查看:88
本文介绍了Collection.get返回的值在ribs.js中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对collection.get和model.get返回未定义有疑问.

I have issue with collection.get and model.get returning undefined.

这是我的初始化代码

initialize: function () {
    this.collection = new productsCollection();
    this.model = new productModel();
}

这是我的渲染代码

this.collection.fetch({
    success: function (product) {
        console.log(product);
        $(that.el).html(_.template(productListTemplate, { products: product.models, _: _ }));
    }
});

我的产品列表显示良好.当我单击每个产品时,都会弹出一个可以更改名称的弹出窗口

my list of products is displaying fine. when i click on each product i get a popup where name can be changed

我想在模型中获取设置的新名称并触发保存

i want to get the set new name in the model and trigger save

但是我无法获得产品的型号,这里是代码

but i am unable to get the model of the product here is the code

$("#productName").val($(e.currentTarget).html());

var ID = $(e.currentTarget).data("id");
var item = this.collection.get(ID);

console.log("start..........");
console.log(item);
console.log(ID)
//            console.log(this.collection);
console.log(this.model.get(item));
console.log("end..........");

$('.modal').modal('toggle');

我能够在控制台中获得正确的ID,但不能获得集合和模型

I am able to get the correct id in console but not collections and models

能帮一下忙吗

更新 这是完整的查看代码

UPDATE here is the complete view code

function ($, _, Backbone, popupModal, productTab, productsCollection, productListTemplate, productModel) {
    var productListView = Backbone.View.extend({
        el: $("#page"),
        initialize: function () {
            this.collection = new productsCollection();
            this.model = new productModel();
            this.model.bind('change', this.loadResults, this);
        },
        render: function () {
            this.loadResults();
        },
        loadResults: function () {
            var that = this;
            this.collection.fetch({
                success: function (product) {
                    console.log(product);
                    $(that.el).html(_.template(productListTemplate, { products: product.models, _: _ }));
                }
            });
            var modalWindow = $(".modal").modal({
                show: false,
                backdrop: true,
                closeOnEscape: true
            });
            $('#createProduct').click(function (e) {
                this.modalWindow.modal('show');
            });
        },
        // This will simply listen for scroll events on the current el
        events: {
            "click #saveProduct": "saveProduct",
            "click .productTabs": "productTabs",
            "click .productDetails": "productDetails"
        },
        saveProduct: function () {
            this.model.set({
                Name: $('#productName').val()
            });
            this.model.save({ id: this.model.get('id') },
         {
             success: function (model, response) {
                 //                 console.log("success");
             },
             error: function (model, response) {
                 //                 console.log(response);
                 var errorMsg = JSON.parse(response.responseText);
                 $(".errorMessage").html('<div class="alert alert-error">' + errorMsg.Error + '</div>');
             }
         });
        },
        productTabs: function (e) {
            e.preventDefault();
            $(this).tab('show');
        },
        productDetails: function (e) {
            e.preventDefault();
            $("#productName").val($(e.currentTarget).html());
            var ID = $(e.currentTarget).data("id");
            var item = this.collection.get(ID);
            console.log("start..........");
            console.log(item);
            console.log(ID)
            //            console.log(this.collection);
            console.log(this.collection.models.get(item));
            console.log("end..........");
            $('.modal').modal('toggle');
        }
    });
    return new productListView;
});

更新回复

this.collection

b.hasOwnProperty.e
_byCid: Object
_byId: Object
_onModelEvent: function () { [native code] }
_removeReference: function () { [native code] }
length: 2
models: Array[2]
0: b.hasOwnProperty.e
1: b.hasOwnProperty.e
length: 2
__proto__: Array[0]
__proto__: s​

它有2种型号,我的清单中也有2种产品

It has 2 models and my list also has 2 products

this.model

_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
ID: ""
Name: ""
hRef: ""
__proto__: Object
cid: "c2"
__proto__: s

属性为空

给了我以下

cid: "view1"
collection: b.hasOwnProperty.e
_byCid: Object
_byId: Object
_onModelEvent: function () { [native code] }
_removeReference: function () { [native code] }
length: 2
models: Array[2]
__proto__: s
model: b.hasOwnProperty.e
_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
cid: "c2"
__proto__: s
options: Object
__proto__: s

更新,这是我扩展收藏集时看到的内容

UPDATE This is what i see when i expand my collections

b.hasOwnProperty.e
_byCid: Object
_byId: Object
_onModelEvent: function () { [native code] }
_removeReference: function () { [native code] }
length: 2
models: Array[2]
0: b.hasOwnProperty.e
_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
ID: "7e0c94fc-7c16-45c9-84a9-a0690103b946"
Name: "dsa"
hRef: "Product/dsa"
__proto__: Object
cid: "c3"
collection: b.hasOwnProperty.e
__proto__: s
1: b.hasOwnProperty.e
length: 2
__proto__: Array[0]
__proto__: s

推荐答案

问题是您必须将所有通过DOM事件调用的函数绑定到视图实例:

The problem is that you have to bind all function that will call by DOM event, to the instance of your view:

因此将此行添加到您的initialize方法中:

So add this line into your initialize method:

_.bindAll(this, "saveProduct", "productTabs", "productDetails")

否则,函数中的this将是全局window对象,而不是视图实例.

Otherwise this in the function will be global window object instead of the instance of your view.

这篇关于Collection.get返回的值在ribs.js中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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