A Question about Creating custom bindings

92 views
Skip to first unread message

lighting.cui

unread,
May 7, 2012, 4:52:22 AM5/7/12
to knock...@googlegroups.com
Hi all,

Try use data-bind add a swf file to a div. 

i created a custom bindings:
(function(){ 
     ko.bindingHandlers['flash'] = {
        'update': function(element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor()); 
            swfobject.embedSWF(value, element.id, "200", "120", "9.0.0"); 
        }
     };
})();
 
html code:
<div id="swf-here"  data-bind="flash : swfurl"></div>

js code:
var viewModel = { 
 swfurl: ko.observable("/flashs/1.swf") 
}; 
ko.applyBindings(viewModel);

while page initialization the 'update' execute once , then try call function as follow ,but  'update'  not execute as expected
function gos1(){
    viewModel.swfurl('/flashs/1.swf'); 
}



BTW: 

1). it not work until i called  function as follow :
function update( 
    ko.bindingHandlers.flash.update(document.getElementById("swf-here"),viewModel.swfurl);
}
 
2). if change value  immediately after applyBindings,update can expected code as follow:
var viewModel = {   
   swfurl: ko.observable("/flashs/1.swf") 
}; 
ko.applyBindings(viewModel);
viewModel.swfurl('/flashs/2.swf'); 
 

Lighting Cui

unread,
May 8, 2012, 11:32:16 AM5/8/12
to KnockoutJS
any update?

rpn

unread,
May 8, 2012, 11:37:04 AM5/8/12
to knock...@googlegroups.com
Hello-
A question about your first example:  Are you changing the value to the same value (1.swf) that is already in the observable?

var viewModel = { 
 swfurl: ko.observable("/flashs/1.swf") 
}; 
ko.applyBindings(viewModel);

function gos1(){
    viewModel.swfurl('/flashs/1.swf'); 
}

If that is the case, then no subscribers will be notified, as the value did not change.  You can force notifications though by calling: viewModel.swfurl.valueHasMutated();



On Tuesday, May 8, 2012 10:32:16 AM UTC-5, Lighting Cui wrote:
any update?

Lighting Cui

unread,
May 8, 2012, 1:10:16 PM5/8/12
to KnockoutJS
oh sorry , first example should be
function gos1(){
viewModel.swfurl('/flashs/2.swf');
}




On 5月8日, 下午11时37分, rpn <rnieme...@gmail.com> wrote:
> Hello-
> A question about your first example:  Are you changing the value to the
> same value (1.swf) that is already in the observable?
>
> var viewModel = {
>
> >  swfurl: ko.observable("/flashs/1.swf")
>
> > };
>
> > ko.applyBindings(viewModel);
>
> function gos1(){
>
> >     viewModel.swfurl('/flashs/1.swf');
>
> > }
>
> If that is the case, then no subscribers will be notified, as the value did
> not change.  You can force notifications though by calling: *
> viewModel.swfurl.valueHasMutated();*

Lighting Cui

unread,
May 8, 2012, 1:16:28 PM5/8/12
to KnockoutJS
i debug the code i think really matter is i use 'swfobject',
when swfobject.embedSWF() execute,
it will create a new object to replace the div ,
<object type="application/x-shockwave-flash" data="/flashs/1.swf"
width="200" height="120" id="aa" style="visibility: visible; "></
object>
and the new object not has attribute''data-bind"

in that way .. how to rebind


On 5月8日, 下午11时37分, rpn <rnieme...@gmail.com> wrote:
> Hello-
> A question about your first example:  Are you changing the value to the
> same value (1.swf) that is already in the observable?
>
> var viewModel = {
>
> >  swfurl: ko.observable("/flashs/1.swf")
>
> > };
>
> > ko.applyBindings(viewModel);
>
> function gos1(){
>
> >     viewModel.swfurl('/flashs/1.swf');
>
> > }
>
> If that is the case, then no subscribers will be notified, as the value did
> not change.  You can force notifications though by calling: *
> viewModel.swfurl.valueHasMutated();*

Casey Corcoran

unread,
May 8, 2012, 1:44:45 PM5/8/12
to knock...@googlegroups.com
Correct, SWFObject replaces whatever element you specify in the "id"
parameter. You can just create a child node for the swf like so:

http://jsfiddle.net/snaptopixel/8zpQ5/

Lighting Cui

unread,
May 8, 2012, 10:44:25 PM5/8/12
to KnockoutJS
Thank you so much Casey

This is what I want
Reply all
Reply to author
Forward
0 new messages