![](/img/trans.png)
[英]Marionette.CompositeView, how to pass parameters to Marionette.ItemView
[英]How to pass a UI element to a new fabric canvas within a Marionette ItemView
我想使用Fabric.js和Marionette.js v2.4.7。
我有错误
Cannot create property 'style' on string 'constructor'
我正在尝试使用this.ui.canva
但出现错误
Cannot set property 'userSelect' of undefined
define([
'app',
'marionette',
'backbone',
'underscore',
'pace',
'text!templates/constructor/index.ejs',
'fabric'
], function (App, Marionette, Backbone, _, pace, constructorTmpl, fabric) {
'use strict';
return Marionette.ItemView.extend({
template: _.template(constructorTmpl),
ui: {
canva: '#constructor'
},
onRender: function () {
var canvas = new fabric.Canvas('constructor');
}
});
});
<canvas id="constructor" width="300" height="300"></canvas>
由于视图的元素尚不在DOM中,因此fabric无法全局找到ID constructor
的元素。
但是该元素已经存在于视图内部的内存中,您可以使用它。 fabric.Canvas
可以使用DOM元素代替字符串。
在v3之前的Marionette版本中, ItemView
负责调用bindUIElements
并且可以直接在ui
hash中使用它们 。
onRender: function () {
var canvas = new fabric.Canvas(this.ui.canva[0]);
}
注意:从Marionette v3.x开始,
Marionette.View
替换了Marionette.LayoutView
和Marionette.ItemView
。
使用getUI
视图函数获取UI元素的jQuery对象。
onRender: function () {
var canvas = new fabric.Canvas(this.getUI('canva')[0]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.