gpt4 book ai didi

javascript - 如何将值从主视图传递到 subview Backbone.js

转载 作者:行者123 更新时间:2023-11-29 21:42:58 26 4
gpt4 key购买 nike

主视图

     events: {
"click .open-sku-details":"openSkuDetails"

},
openSkuDetails: function(ev) {
var self = this;
var sku_id = $(ev.target).attr('sku_id');
self.skuDetailsModel.set("id",sku_id);
self.skuDetailsModel.fetch({
}).done(function (response) {
});

this.skuDetails = new skuDetailsView({model:self.skuDetailsModel});
return this.skuDetails.render();

}

subview

    var $ = jQuery = require('jquery'),
Backbone = require('backbone'),
Handlebars = require('handlebars'),
_ = require('underscore'),
skuDetailsTemplate = require("../../templates/product/SkuDetails.html"),
skuDetailsModel = require('../../models/product/SkuDetailsModel');
var SkuDetailsView = Backbone.View.extend({
el: ".sku-details-container",
tagName:"div",

initialize: function () {
var self = this;
this.skuDetailsModel = new skuDetailsModel();
this.listenTo(self.skuDetailsModel, 'add', self.render);
this.listenTo(self.skuDetailsModel, 'change', self.render);
self.skuDetailsModel.fetch({
}).done(function (response) {
});
},
render: function () {
var self = this;
this.$el.html(skuDetailsTemplate({
skuDetails: self.skuDetailsModel.toJSON(),
}));
}

});

module.exports = SkuDetailsView;

如何将一个 View 的 id 值传递给另一个 View ?

最佳答案

通过 the options argument 传递.

在你的主视图中

this.skuDetails = new skuDetailsView({
model:self.skuDetailsModel,
sku_id: sku_id
});

在你的 subview 中

initialize: function (options) {
var sku_id = options.sku_id;
}

选项对象仅在初始化函数中可用。存储它,例如使用 this.sku_id = options.sku_id,如果您在渲染时需要它。

当然,您也可以通过模型将其作为属性传递,但如果它只是与 View 相关,则将其作为选项传递更有意义。

关于javascript - 如何将值从主视图传递到 subview Backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32077596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com