Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
rivets-error-binder
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
27ac8600
authored
2016-09-01 14:12:18 -0500
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Rewrite the holder to be a real object, that uses Backbone.Events to
dispatch everything.
1 parent
0e7055a1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
33 deletions
src/scripts/rivets-error-binder.js
src/scripts/rivets-error-binder.js
View file @
27ac860
...
...
@@ -2,6 +2,7 @@ define(function(require) {
'use strict'
;
var
module
=
require
(
'module'
);
var
rivets
=
require
(
'rivets'
);
var
Backbone
=
require
(
'backbone'
);
var
$
=
require
(
'jquery'
);
var
rivetsBinderCall
=
function
(
binding
,
binderName
,
methodName
,
args
)
{
...
...
@@ -32,57 +33,65 @@ define(function(require) {
}
};
rivets
.
binders
[
'error-*'
]
=
{
bind
:
function
(
el
)
{
var
observer
=
this
.
observer
;
function
createTriggerProxy
(
obj
,
eventName
)
{
return
function
()
{
obj
.
trigger
.
apply
(
obj
,
[
eventName
].
concat
(
_
.
toArray
(
arguments
)));
};
}
function
ErrorHandler
(
binding
,
el
)
{
var
seen
=
{};
var
observer
=
binding
.
observer
;
var
oldParent
;
var
parentObserver
;
var
keyPath
=
observer
.
key
.
path
;
var
holder
=
this
.
validationHolder
=
{
//marker: el.parentNode.insertBefore(document.createComment(" rivets: " + this.type + " "), el),
observer
:
observer
,
seen
:
{},
focus
:
function
()
{
this
.
on
(
'bind'
,
function
()
{
render
(
el
,
'bind'
);
render
(
el
,
'validated'
,
false
);
});
this
.
on
(
'focus'
,
function
()
{
render
(
el
,
'focus'
,
false
);
},
blur
:
function
()
{
holder
.
seen
[
keyPath
]
=
true
;
});
this
.
on
(
'blur'
,
function
()
{
seen
[
keyPath
]
=
true
;
render
(
el
,
'blur'
,
false
);
if
(
observer
.
target
)
{
observer
.
target
.
validate
();
}
},
validated
:
function
(
isValid
,
model
,
errors
)
{
});
this
.
on
(
'validated'
,
function
(
isValid
,
model
,
errors
)
{
var
errorList
=
errors
[
keyPath
];
if
(
errorList
&&
holder
.
seen
[
keyPath
])
{
if
(
errorList
&&
seen
[
keyPath
])
{
render
(
el
,
'validated'
,
errorList
);
}
else
{
render
(
el
,
'validated'
,
false
);
}
});
this
.
on
(
'unbind'
,
function
()
{
render
(
el
,
'unbind'
);
$
(
el
).
off
(
'.rivets-error-binder'
);
});
var
$el
=
$
(
el
);
$el
.
on
(
'focus.rivets-error-binder'
,
createTriggerProxy
(
this
,
'focus'
));
$el
.
on
(
'blur.rivets-error-binder'
,
createTriggerProxy
(
this
,
'blur'
));
if
(
observer
.
target
)
{
this
.
listenTo
(
observer
.
target
,
'validated'
,
createTriggerProxy
(
this
,
'validated'
));
}
}
};
$
(
el
).
on
(
'focus.rivets-error-binder'
,
holder
.
focus
).
on
(
'blur.rivets-error-binder'
,
holder
.
blur
);
_
.
extend
(
ErrorHandler
.
prototype
,
Backbone
.
Events
);
rivets
.
binders
[
'error-*'
]
=
{
bind
:
function
(
el
)
{
this
.
errorHandler
=
new
ErrorHandler
(
this
,
el
);
this
.
errorHandler
.
trigger
(
'bind'
);
rivetsBinderCall
(
this
,
this
.
args
[
0
],
'bind'
,
arguments
);
render
(
el
,
'bind'
);
render
(
el
,
'validated'
,
false
);
},
unbind
:
function
(
el
)
{
var
holder
=
this
.
validationHolder
;
render
(
el
,
'unbind'
);
$
(
this
.
validationHolder
.
marker
).
after
(
el
).
remove
();
$
(
el
).
off
(
'.rivets-error-binder'
);
if
(
holder
.
observer
.
target
)
{
holder
.
observer
.
target
.
off
(
'validated'
,
holder
.
validated
);
}
delete
this
.
validationHolder
;
this
.
errorHandler
.
trigger
(
'unbind'
);
delete
this
.
errorHandler
;
rivetsBinderCall
(
this
,
this
.
args
[
0
],
'unbind'
,
arguments
);
},
routine
:
function
()
{
var
holder
=
this
.
validationHolder
;
if
(
this
.
observer
.
target
)
{
this
.
observer
.
target
.
off
(
'validated'
,
holder
.
validated
);
}
if
(
this
.
observer
.
target
)
{
this
.
observer
.
target
.
on
(
'validated'
,
holder
.
validated
);
}
rivetsBinderCall
(
this
,
this
.
args
[
0
],
'routine'
,
arguments
);
}
};
...
...
Please
register
or
sign in
to post a comment