Implement configuration option for setting a data attribute prefix. [Closes #4]
Showing
2 changed files
with
24 additions
and
5 deletions
... | @@ -60,11 +60,23 @@ | ... | @@ -60,11 +60,23 @@ |
60 | 60 | ||
61 | this.build = __bind(this.build, this); | 61 | this.build = __bind(this.build, this); |
62 | 62 | ||
63 | this.bindingRegExp = __bind(this.bindingRegExp, this); | ||
64 | |||
63 | this.build(); | 65 | this.build(); |
64 | } | 66 | } |
65 | 67 | ||
68 | View.prototype.bindingRegExp = function() { | ||
69 | var prefix; | ||
70 | prefix = Rivets.config.prefix; | ||
71 | if (prefix) { | ||
72 | return new RegExp("^data-" + prefix + "-"); | ||
73 | } else { | ||
74 | return /^data-/; | ||
75 | } | ||
76 | }; | ||
77 | |||
66 | View.prototype.build = function() { | 78 | View.prototype.build = function() { |
67 | var attribute, context, keypath, node, path, type, _i, _len, _ref, _results; | 79 | var attribute, context, dataRegExp, keypath, node, path, type, _i, _len, _ref, _results; |
68 | this.bindings = []; | 80 | this.bindings = []; |
69 | _ref = this.el.getElementsByTagName('*'); | 81 | _ref = this.el.getElementsByTagName('*'); |
70 | _results = []; | 82 | _results = []; |
... | @@ -76,8 +88,9 @@ | ... | @@ -76,8 +88,9 @@ |
76 | _results1 = []; | 88 | _results1 = []; |
77 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | 89 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { |
78 | attribute = _ref1[_j]; | 90 | attribute = _ref1[_j]; |
79 | if (/^data-/.test(attribute.name)) { | 91 | dataRegExp = new RegExp(this.data, 'g'); |
80 | type = attribute.name.replace('data-', ''); | 92 | if (this.bindingRegExp().test(attribute.name)) { |
93 | type = attribute.name.replace(this.bindingRegExp(), ''); | ||
81 | path = attribute.value.split('.'); | 94 | path = attribute.value.split('.'); |
82 | context = this.contexts[path.shift()]; | 95 | context = this.contexts[path.shift()]; |
83 | keypath = path.join('.'); | 96 | keypath = path.join('.'); | ... | ... |
... | @@ -37,14 +37,20 @@ class Rivets.View | ... | @@ -37,14 +37,20 @@ class Rivets.View |
37 | constructor: (@el, @contexts) -> | 37 | constructor: (@el, @contexts) -> |
38 | @build() | 38 | @build() |
39 | 39 | ||
40 | # The regular expression used to match Rivets.js data binding attributes. | ||
41 | bindingRegExp: => | ||
42 | prefix = Rivets.config.prefix | ||
43 | if prefix then new RegExp("^data-#{prefix}-") else /^data-/ | ||
44 | |||
40 | # Parses and builds new Rivets.Binding instances for the data bindings. | 45 | # Parses and builds new Rivets.Binding instances for the data bindings. |
41 | build: => | 46 | build: => |
42 | @bindings = [] | 47 | @bindings = [] |
43 | 48 | ||
44 | for node in @el.getElementsByTagName '*' | 49 | for node in @el.getElementsByTagName '*' |
45 | for attribute in node.attributes | 50 | for attribute in node.attributes |
46 | if /^data-/.test attribute.name | 51 | dataRegExp = new RegExp(@data, 'g') |
47 | type = attribute.name.replace 'data-', '' | 52 | if @bindingRegExp().test attribute.name |
53 | type = attribute.name.replace @bindingRegExp(), '' | ||
48 | path = attribute.value.split '.' | 54 | path = attribute.value.split '.' |
49 | context = @contexts[path.shift()] | 55 | context = @contexts[path.shift()] |
50 | keypath = path.join '.' | 56 | keypath = path.join '.' | ... | ... |
-
Please register or sign in to post a comment