Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bindings: The Zen of Montage

Avatar for Kris Kowal Kris Kowal
February 06, 2013

Bindings: The Zen of Montage

Montage is a web application framework that places an emphasis on reacting to changes to properties of objects and the content of collections. FRB are Functional Reactive Bindings, the new implementation of bindings coming into Montage.

Avatar for Kris Kowal

Kris Kowal

February 06, 2013
Tweet

Other Decks in Technology

Transcript

  1. function Point(x, y) { this.x = x; this.y = y;

    } Point.prototype.magnitude = function () { return Math.sqrt( this.x * this.x + this.y * this.y ); }; var point = new Point(3, 4); expect(point.magnitude()).toEqual(5);
  2. var Montage = require("montage").Montage; var Point = Montage.create(Montage, { didCreate:

    { value: function () { this.addOwnPropertyChangeListener("x", this, "coordinate"); this.addOwnPropertyChangeListener("y", this, "coordiante"); } }, handleCoordinateChange: { value: function () { if (this.x == null || this.y == null) { this.magnitude = null; } else { this.magnitude = Math.sqrt( this.x * this.x + this.y * this.y ); } } } }); var point = Point.create(); point.x = 3; point.y = 4; expect(point.magnitude).toEqual(5);
  3. var Montage = require("montage").Montage; var Point = Montage.create(Montage, { didCreate:

    { value: function () { this.defineBinding("magnitude", { "<-": "(x ** 2 + y ** 2) // 2" }); } } }); var point = Point.create(); point.x = 3; point.y = 4; expect(point.magnitude).toEqual(5)
  4. Array Range Changes shift() → splice(0, 1) unshift(...values) → splice(0,

    0, ...values) push(...values) → splice(length, 0, ...values) pop() → splice(length - 1, 1) clear() → splice(0, length) set(index, value) → splice(index, 1, value) swap(i, l, values) → splice(i, l, ...values)
  5. Bindings a ↔ b a.b ↔ c.d a ← b.map{c}

    a ← b.filter{c} a ← b.sorted{c} a ← b.flatten() sum, average, reversed, enumerate, items, some, every a.has(b) ↔ c a == b ↔ c a[b] ↔ c a.* ← c.map{d} a[*] ← b.toMap() a ← b.toMap{[.0, .1]} r ← (x**2 + y**2) //2 f ↔ c * 1.8 + 32 a ← b.{x: .0, y: .1}
  6. a.b ↔ c.d this.defineBinding("a.b", { "<->": "c.d" }); { "prototype":

    "montage", "bindings": { "a.b": {"<->": "c.d"} } }
  7. "selectionName": { "prototype": "montage/ui/dynamic-text.reel", "properties": { "element": {"#": "selectionName"} },

    "bindings": { "value": { "<-": " @selection.currentIteration.object.name + ( [email protected] ? ', ' : '' ) " } } }
  8. function observeProperty(object, key, emit, source, parameters) { var cancel =

    Function.noop; function propertyChange(value, key, object) { cancel(); cancel = emit(value, key, object) || Function.noop; } PropertyChanges.addOwnPropertyChangeListener( object, key, propertyChange ); propertyChange(object[key], key, object); return once(function cancelPropertyObserver() { cancel(); PropertyChanges.removeOwnPropertyChangeListener( object, key, propertyChange ); }); }
  9. exports.makeSumObserver = makeCollectionObserverMaker( function setup() { var sum = 0;

    return function rangeChange(plus, minus, index) { sum += plus.sum() - minus.sum(); return sum; }; } );