javascript - Why my applyBindings doesn't work? Knockout -


hello trying create input , iframe , when paste youtube link iframe should change new src. have done far

<div class="heading">id <input data-bind="text: youtubelink"/></div> <iframe id="player" type="text/html" width="444" height="250" frameborder="0" data-bind="attr: { src: linkembed }"></iframe> 

and in script:

function myviewmodel() {     this.youtubelink = ko.observable('https://www.youtube.com/watch?v=4unkmlckw9m');     this.linkembed = ko.purecomputed({         read: function () {                 var extract = this.youtubelink().replace("/watch?v=", "/embed/");                 console.log(extract)                 return extract;         },         write: function (value) {                  this.youtubelink();         },         owner:     }); } ko.applybindings(myviewmodel()); 

this works want video wont change if paste link in input.

i using knockout documentation: http://knockoutjs.com/documentation/computed-writable.html

you have several problems:

  1. you don't call new on model, wrote constructor
  2. you use text binding instead of value binding input
  3. your computed's write doesn't assign, don't need anyway

once correct those, works.

function myviewmodel() {     var model = {};     model.youtubelink = ko.observable('https://www.youtube.com/watch?v=4unkmlckw9m');     model.linkembed = ko.purecomputed(function () {         var result = model.youtubelink().replace("/watch?v=", "/embed/")         return result;     });     return model; } ko.applybindings(myviewmodel()); 

http://jsfiddle.net/ueoob7ne/2/


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

html - How can i make an element from a bottom stacking context stays in front of another higher stacking context? -

html - href attribute breaking an element -