admin管理员组文章数量:1414621
I am using Knockout.js to display a list of search results returned from my server. Each result from the result set contains an image. I'm trying to attach a handler to the load event for each image so I can resize the image's parent div based on max height of all the images but the load event seems to be firing before the images have finished loading.
Furthermore, I can see the load handler is hit in firebug, but the act of debugging just gives the images time to load so everything works properly when debugging.
The load handler within my viewModel: This will push a resolved deferred into my promises array. Then all I have to do is check to make sure the array is full and that will tell me the images have loaded.
self.imageLoadHandler = function() {
var promise = $.Deferred().resolve;
promises.push(promise);
};
The binding for the load handler
<img data-bind="attr: { src: Posters.Detailed, alt: Title }, event: { load: $root.imageLoadHandler }" />
The search function within my viewModel: This returns the json results, populates my observable array and then sets the height of each "thumbnail" by first checking to make sure the promises array is full, then when it is, gets the max height and sets all "thumbnails" to that height.
self.search = function () {
$.getJSON(arguments[0].action, { name: this.searchValue() }, function (data) {
self.movies(data);
setThumbnailHeight();
});
};
var setThumbnailHeight = function() {
var $items = $('.thumbnails li');
$.when.apply($, promises).done(function() {
var maxHeight = Math.max.apply(null, $items.map(function () {
return $(this).height();
}).get());
$items.css('height', maxHeight);
});
};
My question is: Why is the load event firing before the image load and how can I have the load event fire at the appropriate time?
UPDATE
My thought is that the load event may be firing prematurely because it's possible that it's attached after the image has it's src
. I may have to create a custom binding if simply changing the order of the event bindings doesn't work.
I am using Knockout.js to display a list of search results returned from my server. Each result from the result set contains an image. I'm trying to attach a handler to the load event for each image so I can resize the image's parent div based on max height of all the images but the load event seems to be firing before the images have finished loading.
Furthermore, I can see the load handler is hit in firebug, but the act of debugging just gives the images time to load so everything works properly when debugging.
The load handler within my viewModel: This will push a resolved deferred into my promises array. Then all I have to do is check to make sure the array is full and that will tell me the images have loaded.
self.imageLoadHandler = function() {
var promise = $.Deferred().resolve;
promises.push(promise);
};
The binding for the load handler
<img data-bind="attr: { src: Posters.Detailed, alt: Title }, event: { load: $root.imageLoadHandler }" />
The search function within my viewModel: This returns the json results, populates my observable array and then sets the height of each "thumbnail" by first checking to make sure the promises array is full, then when it is, gets the max height and sets all "thumbnails" to that height.
self.search = function () {
$.getJSON(arguments[0].action, { name: this.searchValue() }, function (data) {
self.movies(data);
setThumbnailHeight();
});
};
var setThumbnailHeight = function() {
var $items = $('.thumbnails li');
$.when.apply($, promises).done(function() {
var maxHeight = Math.max.apply(null, $items.map(function () {
return $(this).height();
}).get());
$items.css('height', maxHeight);
});
};
My question is: Why is the load event firing before the image load and how can I have the load event fire at the appropriate time?
UPDATE
My thought is that the load event may be firing prematurely because it's possible that it's attached after the image has it's src
. I may have to create a custom binding if simply changing the order of the event bindings doesn't work.
1 Answer
Reset to default 5You may be able to fix the event binding problem simply by putting your event:
data before the attr:
data.
In any event (no pun intended) this line is wrong:
var promise = $.Deferred().resolve;
it creates a deferred, and then assigns promise
as a reference to the resolve
function, without invoking it.
When the resulting array is passed to $.when
the bined promise is resolved immediately, since objects that don't themselves implement the promise
interface are implicitly considered resolved.
You need to create the array of deferred objects (of the desired length) outside of the imageLoadHandler
, and then explicitly call .resolve()
on the appropriate deferred object at that point.
本文标签: javascriptKnockoutjs event binding to image load eventStack Overflow
版权声明:本文标题:javascript - Knockoutjs event binding to image load event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745154513a2645088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论