最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I get size (heightwidth) information in Backbone Views? - Stack Overflow

programmeradmin1浏览0评论

In the render function of my backbone app, I want to fill the rest of the table with empty rows for the alternating row look, but I dont know how many rows to add. I tried this.el.clientHeight, $(this.el).height(), and all other permutations of such functions to have them all return 0. Does anyone know how to do this? I also dont want to add too many in order not to introduce a scroll bar.


var bar = Backbone.View.extend({
    initialize: function(){
        _.bindAll(this, "render");
    },

    render: function(){
        var html = "<h1>Hi!</h1>";
        $(this.el).html(html);
        var myWidth = this.el.width();
        alert(myWidth);
        return this;
    }

});



var foo = Backbone.View.extend({
    el: $('#myElement'),

    initialize: function(){
        _.bindAll(this, "render");
        this.subview = new bar();
    },

    render: function(){
        $(this.el).append(this.subview.render().el);
        return this;
    }

});




Solution for those stuck with the same problem as I had: You have to wait for the elements to be fully attached to the dom in order to get height/width info. In order to resolve this I added a postrender call after the render is fully done which went back and handled any small details.

In the render function of my backbone app, I want to fill the rest of the table with empty rows for the alternating row look, but I dont know how many rows to add. I tried this.el.clientHeight, $(this.el).height(), and all other permutations of such functions to have them all return 0. Does anyone know how to do this? I also dont want to add too many in order not to introduce a scroll bar.


var bar = Backbone.View.extend({
    initialize: function(){
        _.bindAll(this, "render");
    },

    render: function(){
        var html = "<h1>Hi!</h1>";
        $(this.el).html(html);
        var myWidth = this.el.width();
        alert(myWidth);
        return this;
    }

});



var foo = Backbone.View.extend({
    el: $('#myElement'),

    initialize: function(){
        _.bindAll(this, "render");
        this.subview = new bar();
    },

    render: function(){
        $(this.el).append(this.subview.render().el);
        return this;
    }

});




Solution for those stuck with the same problem as I had: You have to wait for the elements to be fully attached to the dom in order to get height/width info. In order to resolve this I added a postrender call after the render is fully done which went back and handled any small details.

Share Improve this question edited Nov 23, 2011 at 9:46 chacham15 asked Nov 22, 2011 at 11:15 chacham15chacham15 14.3k26 gold badges122 silver badges218 bronze badges 1
  • Btw do you realize that your storing the width in a variable called myHeight? – Jack Commented Nov 22, 2011 at 19:05
Add a ment  | 

3 Answers 3

Reset to default 4

On some browsers, the width and height information will not be immediately available right after setting the html. The reason for this is that you have updated the DOM, but the browser may not have laid out the new page yet.

In the past I have resorted to deferring the call by a tick so that the browser has time to catch up before you measure it. You can use underscore.defer() for this. For example:

render: function(){
  var html = "<h1>Hi!</h1>";
  $(this.el).html(html);
  _.defer(_.bind(function() {  
    alert($(this.el).width());
  }, this);
  return this;
}

Also, jQuery.width() will probably give you better patibility than clientWidth. Good luck!

Try getting the height after you attach it, (so in "foo" as opposed to "bar").

For example in "foo"

    render: function(){

    $(this.el).append(this.subview.render().el);
    alert($(this.subview.el).height());
    return this;
}

Your view should look something like this:

var myView = Backbone.View.extend({
    el: $('#myElement'),

    initialize: function(){
        _.bindAll(this, "render");
    },

    render: function(){
        var myHeight = this.el.height();
        alert(myHeight);
        return this;
    }

});

Hope that helps

发布评论

评论列表(0)

  1. 暂无评论