clientIO / joint

A proven SVG-based JavaScript diagramming library powering exceptional UIs
https://jointjs.com
Mozilla Public License 2.0
4.73k stars 853 forks source link

fix(mvc.View): set style via options #2705

Closed kumilingus closed 4 months ago

kumilingus commented 4 months ago

Description

Allow style attribute to be set via options.

All other properties can be set either as a class property or as a constructor option.

// Already supported
(new mvc.View.extend({ style: { color: 'red' }})).el.style.color === 'red';
// Supported after this PR
(new mvc.View({ style: { color: 'red' }})).el.style.color === 'red'

Documentation

The documentation missing not only the style property but also events and constructor(). https://docs.jointjs.com/api/mvc/View/#properties

style

A hash of CSS properties that will be set as inline style on the view's el (color: red;, etc.), or a function that returns such a hash.

constructor()

There are several special options that, if passed, will be attached directly to the view: model, collection, el, id, className, tagName, attributes, style and events. If the view defines an initialize function, it will be called when the view is first created. If you'd like to create a view that references an element already in the DOM, pass in the element as an option: new View({el: existingElement})

var doc = documents.first();

new DocumentRow({
  model: doc,
  id: "document-row-" + doc.id
});

events

The events hash (or method) can be used to specify a set of DOM events that will be bound to methods on your View through delegateEvents.

JointJS will automatically attach the event listeners at instantiation time, right before invoking initialize.

var ENTER_KEY = 13;
var InputView = Backbone.View.extend({

  tagName: 'input',

  events: {
    "keydown" : "keyAction",
  },

  render: function() { ... },

  keyAction: function(e) {
    if (e.which === ENTER_KEY) {
      this.collection.add({text: this.$el.val()});
    }
  }
});