750bf3ef by Michael Richards

Use the Rivets namespace for the Binding class, core bindings and default config…

…. Add a public configure function.
1 parent 8ef7c842
1 // Generated by CoffeeScript 1.3.1 1 // Generated by CoffeeScript 1.3.1
2 (function() { 2 (function() {
3 var Rivets, 3 var Rivets, attributeBinding, bidirectionals, getInputValue, rivets, stateBinding,
4 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, 4 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
5 __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; 5 __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
6 6
7 Rivets = { 7 Rivets = {};
8 Helpers: {}
9 };
10 8
11 Rivets.Binding = (function() { 9 Rivets.Binding = (function() {
12 10
...@@ -22,7 +20,7 @@ ...@@ -22,7 +20,7 @@
22 20
23 this.set = __bind(this.set, this); 21 this.set = __bind(this.set, this);
24 22
25 this.routine = Rivets.bindings[this.type] || Rivets.Helpers.attributeBinding(this.type); 23 this.routine = Rivets.bindings[this.type] || attributeBinding(this.type);
26 } 24 }
27 25
28 Binding.prototype.set = function(value) { 26 Binding.prototype.set = function(value) {
...@@ -38,9 +36,9 @@ ...@@ -38,9 +36,9 @@
38 this.set() && this.adapter.subscribe(this.context, this.keypath, function(value) { 36 this.set() && this.adapter.subscribe(this.context, this.keypath, function(value) {
39 return _this.set(value); 37 return _this.set(value);
40 }); 38 });
41 if (_ref = this.type, __indexOf.call(Rivets.bidirectionals, _ref) >= 0) { 39 if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) {
42 return this.el.addEventListener('change', function(el) { 40 return this.el.addEventListener('change', function(el) {
43 return _this.adapter.publish(_this.context, _this.keypath, Rivets.Helpers.getInputValue(el)); 41 return _this.adapter.publish(_this.context, _this.keypath, getInputValue(el));
44 }); 42 });
45 } 43 }
46 }; 44 };
...@@ -49,7 +47,7 @@ ...@@ -49,7 +47,7 @@
49 47
50 })(); 48 })();
51 49
52 Rivets.Helpers.getInputValue = function(el) { 50 getInputValue = function(el) {
53 switch (el.type) { 51 switch (el.type) {
54 case 'text': 52 case 'text':
55 case 'textarea': 53 case 'textarea':
...@@ -62,7 +60,7 @@ ...@@ -62,7 +60,7 @@
62 } 60 }
63 }; 61 };
64 62
65 Rivets.Helpers.attributeBinding = function(attr) { 63 attributeBinding = function(attr) {
66 return function(el, value) { 64 return function(el, value) {
67 if (value) { 65 if (value) {
68 return el.setAttribute(attr, value); 66 return el.setAttribute(attr, value);
...@@ -72,24 +70,26 @@ ...@@ -72,24 +70,26 @@
72 }; 70 };
73 }; 71 };
74 72
75 Rivets.Helpers.stateBinding = function(attr, inverse) { 73 stateBinding = function(attr, inverse) {
76 if (inverse == null) { 74 if (inverse == null) {
77 inverse = false; 75 inverse = false;
78 } 76 }
79 return function(el, value) { 77 return function(el, value) {
80 var binding; 78 var binding;
81 binding = Rivets.Helpers.attributeBinding(attr); 79 binding = attributeBinding(attr);
82 return binding(el, inverse === !value ? attr : false); 80 return binding(el, inverse === !value ? attr : false);
83 }; 81 };
84 }; 82 };
85 83
84 bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
85
86 Rivets.bindings = { 86 Rivets.bindings = {
87 checked: Rivets.Helpers.stateBinding('checked'), 87 checked: stateBinding('checked'),
88 selected: Rivets.Helpers.stateBinding('selected'), 88 selected: stateBinding('selected'),
89 disabled: Rivets.Helpers.stateBinding('disabled'), 89 disabled: stateBinding('disabled'),
90 unchecked: Rivets.Helpers.stateBinding('checked', true), 90 unchecked: stateBinding('checked', true),
91 unselected: Rivets.Helpers.stateBinding('selected', true), 91 unselected: stateBinding('selected', true),
92 enabled: Rivets.Helpers.stateBinding('disabled', true), 92 enabled: stateBinding('disabled', true),
93 text: function(el, value) { 93 text: function(el, value) {
94 return el.innerText = value || ''; 94 return el.innerText = value || '';
95 }, 95 },
...@@ -107,9 +107,23 @@ ...@@ -107,9 +107,23 @@
107 } 107 }
108 }; 108 };
109 109
110 Rivets.bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']; 110 Rivets.config = {
111 preloadData: true
112 };
111 113
112 Rivets["interface"] = { 114 rivets = {
115 configure: function(options) {
116 var property, value, _i, _len, _results;
117 if (options == null) {
118 options = {};
119 }
120 _results = [];
121 for (value = _i = 0, _len = options.length; _i < _len; value = ++_i) {
122 property = options[value];
123 _results.push(Rivets.config[property] = value);
124 }
125 return _results;
126 },
113 register: function(routine, routineFunction) { 127 register: function(routine, routineFunction) {
114 return Rivets.bindings[routine] = routineFunction; 128 return Rivets.bindings[routine] = routineFunction;
115 }, 129 },
...@@ -143,9 +157,9 @@ ...@@ -143,9 +157,9 @@
143 }; 157 };
144 158
145 if (typeof module !== "undefined" && module !== null) { 159 if (typeof module !== "undefined" && module !== null) {
146 module.exports = Rivets["interface"]; 160 module.exports = rivets;
147 } else { 161 } else {
148 this.rivets = Rivets["interface"]; 162 this.rivets = rivets;
149 } 163 }
150 164
151 }).call(this); 165 }).call(this);
......
...@@ -3,12 +3,11 @@ ...@@ -3,12 +3,11 @@
3 # author : Michael Richards 3 # author : Michael Richards
4 # license : MIT 4 # license : MIT
5 5
6 Rivets = 6 Rivets = {}
7 Helpers: {}
8 7
9 class Rivets.Binding 8 class Rivets.Binding
10 constructor: (@el, @adapter, @type, @context, @keypath) -> 9 constructor: (@el, @adapter, @type, @context, @keypath) ->
11 @routine = Rivets.bindings[@type] || Rivets.Helpers.attributeBinding @type 10 @routine = Rivets.bindings[@type] || attributeBinding @type
12 11
13 # Sets a value for this binding. Basically just runs the routine on the 12 # Sets a value for this binding. Basically just runs the routine on the
14 # element with a suplied value. 13 # element with a suplied value.
...@@ -21,42 +20,46 @@ class Rivets.Binding ...@@ -21,42 +20,46 @@ class Rivets.Binding
21 bind: => 20 bind: =>
22 @set() and @adapter.subscribe @context, @keypath, (value) => @set value 21 @set() and @adapter.subscribe @context, @keypath, (value) => @set value
23 22
24 if @type in Rivets.bidirectionals 23 if @type in bidirectionals
25 @el.addEventListener 'change', (el) => 24 @el.addEventListener 'change', (el) =>
26 @adapter.publish @context, @keypath, Rivets.Helpers.getInputValue el 25 @adapter.publish @context, @keypath, getInputValue el
27 26
28 # Returns the current input value for the specified element. 27 # Returns the current input value for the specified element.
29 Rivets.Helpers.getInputValue = (el) -> 28 getInputValue = (el) ->
30 switch el.type 29 switch el.type
31 when 'text', 'textarea', 'password', 'select-one' then el.value 30 when 'text', 'textarea', 'password', 'select-one' then el.value
32 when 'checkbox', 'radio' then el.checked 31 when 'checkbox', 'radio' then el.checked
33 32
34 # Returns an attribute binding routine for the specified attribute. This is what 33 # Returns an attribute binding routine for the specified attribute. This is what
35 # `registerBinding` falls back to when there is no routine for the binding type. 34 # `registerBinding` falls back to when there is no routine for the binding type.
36 Rivets.Helpers.attributeBinding = (attr) -> (el, value) -> 35 attributeBinding = (attr) -> (el, value) ->
37 if value then el.setAttribute attr, value else el.removeAttribute attr 36 if value then el.setAttribute attr, value else el.removeAttribute attr
38 37
39 # Returns a state binding routine for the specified attribute. Can optionally be 38 # Returns a state binding routine for the specified attribute. Can optionally be
40 # negatively evaluated. This is used to build a lot of the core state binding 39 # negatively evaluated. This is used to build a lot of the core state binding
41 # routines. 40 # routines.
42 Rivets.Helpers.stateBinding = (attr, inverse = false) -> (el, value) -> 41 stateBinding = (attr, inverse = false) -> (el, value) ->
43 binding = Rivets.Helpers.attributeBinding(attr) 42 binding = attributeBinding(attr)
44 binding el, if inverse is !value then attr else false 43 binding el, if inverse is !value then attr else false
45 44
45 # Bindings that should also be observed for changes on the DOM element in order
46 # to propogate those changes back to the model object.
47 bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']
48
46 # Core binding routines. 49 # Core binding routines.
47 Rivets.bindings = 50 Rivets.bindings =
48 checked: 51 checked:
49 Rivets.Helpers.stateBinding 'checked' 52 stateBinding 'checked'
50 selected: 53 selected:
51 Rivets.Helpers.stateBinding 'selected' 54 stateBinding 'selected'
52 disabled: 55 disabled:
53 Rivets.Helpers.stateBinding 'disabled' 56 stateBinding 'disabled'
54 unchecked: 57 unchecked:
55 Rivets.Helpers.stateBinding 'checked', true 58 stateBinding 'checked', true
56 unselected: 59 unselected:
57 Rivets.Helpers.stateBinding 'selected', true 60 stateBinding 'selected', true
58 enabled: 61 enabled:
59 Rivets.Helpers.stateBinding 'disabled', true 62 stateBinding 'disabled', true
60 text: (el, value) -> 63 text: (el, value) ->
61 el.innerText = value or '' 64 el.innerText = value or ''
62 html: (el, value) -> 65 html: (el, value) ->
...@@ -68,13 +71,17 @@ Rivets.bindings = ...@@ -68,13 +71,17 @@ Rivets.bindings =
68 hide: (el, value) -> 71 hide: (el, value) ->
69 el.style.display = if value then 'none' else '' 72 el.style.display = if value then 'none' else ''
70 73
71 # Bindings that should also be observed for changes on the DOM element in order 74 # Default configuration.
72 # to propogate those changes back to the model object. 75 Rivets.config =
73 Rivets.bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'] 76 preloadData: true
74 77
75 # The rivets module exposes `register` and `bind` functions to register new 78 # The rivets module exposes `register` and `bind` functions to register new
76 # binding routines and bind contexts to DOM elements. 79 # binding routines and bind contexts to DOM elements.
77 Rivets.interface = 80 rivets =
81 configure: (options={}) ->
82 for property, value in options
83 Rivets.config[property] = value
84
78 register: (routine, routineFunction) -> 85 register: (routine, routineFunction) ->
79 Rivets.bindings[routine] = routineFunction 86 Rivets.bindings[routine] = routineFunction
80 87
...@@ -95,6 +102,6 @@ Rivets.interface = ...@@ -95,6 +102,6 @@ Rivets.interface =
95 102
96 # Exports rivets for both CommonJS and the browser. 103 # Exports rivets for both CommonJS and the browser.
97 if module? 104 if module?
98 module.exports = Rivets.interface 105 module.exports = rivets
99 else 106 else
100 @rivets = Rivets.interface
...\ No newline at end of file ...\ No newline at end of file
107 @rivets = rivets
...\ No newline at end of file ...\ No newline at end of file
......