javascript - JointJS 基本了解

标签 javascript jquery d3.js web jointjs

我在这里关注本教程:

http://jointjs.com/tutorial/html-elements

我有两个小问题:

1)- 这个“joint.util.deepSupplement”是什么?它的目的是什么?

2)- 此自定义 View 在此代码中如何工作?

// Create a custom view for that element that displays an HTML div above it.
// -------------------------------------------------------------------------
joint.shapes.html.ElementView = joint.dia.ElementView.extend({

template: [
    '<div class="html-element">',
    '<button class="delete">x</button>',
    '<label></label>',
    '<span></span>', '<br/>',
    '<select><option>--</option><option>one</option><option>two</option></select>',
    '<input type="text" value="I\'m HTML input" />',
    '</div>'
].join(''),

initialize: function() {
    _.bindAll(this, 'updateBox');
    joint.dia.ElementView.prototype.initialize.apply(this, arguments);

    this.$box = $(_.template(this.template)());
    // Prevent paper from handling pointerdown.
    this.$box.find('input,select').on('mousedown click', function(evt) { evt.stopPropagation(); });
    // This is an example of reacting on the input change and storing the input data in the cell model.
    this.$box.find('input').on('change', _.bind(function(evt) {
        this.model.set('input', $(evt.target).val());
    }, this));
    this.$box.find('select').on('change', _.bind(function(evt) {
        this.model.set('select', $(evt.target).val());
    }, this));
    this.$box.find('select').val(this.model.get('select'));
    this.$box.find('.delete').on('click', _.bind(this.model.remove, this.model));
    // Update the box position whenever the underlying model changes.
    this.model.on('change', this.updateBox, this);
    // Remove the box when the model gets removed from the graph.
    this.model.on('remove', this.removeBox, this);

    this.updateBox();
},
render: function() {
    joint.dia.ElementView.prototype.render.apply(this, arguments);
    this.paper.$el.prepend(this.$box);
    this.updateBox();
    return this;
},
updateBox: function() {
    // Set the position and dimension of the box so that it covers the JointJS element.
    var bbox = this.model.getBBox();
    // Example of updating the HTML with a data stored in the cell model.
    this.$box.find('label').text(this.model.get('label'));
    this.$box.find('span').text(this.model.get('select'));
    this.$box.css({ width: bbox.width, height: bbox.height, left: bbox.x, top: bbox.y, transform: 'rotate(' + (this.model.get('angle') || 0) + 'deg)' });
},
removeBox: function(evt) {
    this.$box.remove();
}
});

我还想知道初始化函数中的前两行在做什么,其中写着_.bindAll,在下一行中,joint.dia.ElementView....?

最佳答案

  • joint.util.deepSupplement 的行为与 lodash _.defaultsDeep ( https://lodash.com/docs#defaultsDeep )
  • joint.dia.ElementView.prototype.initialize.apply(this,arguments); 这是在 js 中调用“基类”方法的方法。在此代码段中,它调用基类 joint.dia.ElementView 上的初始化方法。
  • bindAll:这是用于处理调用上下文('this')的 lodash 语法糖。以下所有语法都是等效的

1. _.bindAll(this, 'updateBox'); this.model.on('change', this.updateBox);

2. this.model.on('change', _.bind(this.updateBox, this));

3. this.model.on('change', this.updateBox, this);

4. var self = this; this.model.on('改变', function() { self.updateBox(); });

关于javascript - JointJS 基本了解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37412646/

相关文章:

javascript - 将选项卡添加到现有选项卡菜单

javascript - 如何使用 JavaScript 或 jQuery 使左右 div 在滚动时粘在顶部?

javascript - d3py 示例代码中出现 IO 错误 - 没有这样的文件或目录 : 'static/d3.js'

javascript - 单击按钮后如何将具有多个类的数据属性值输入到 div?

javascript - c3.js 图表上的数据图标签被 chop

javascript - 如何在图表条上方显示值标签?

javascript - Jquery:如何从选择元素中获取选定的选项

javascript - 如何从 JavaScript 中的数字数组或数字字符串创建整数或数字

javascript - 一页上有多个颜色选择器,带有指向输入框的链接

jquery模拟点击html anchor 标记