1

私はこのコードを実行しようとしています:

var width = 0; 
$('.photos article').each(function(){ width = width + $(this).width();});
$('.photos').width(width);

ビューがレンダリングされ、それを行う方法がわからない場合、すべてのビューとコンテンツを含むコードは次のとおりです。

http://jsbin.com/okezum/6/edit

すべてのビューでコードを実行したいのですが、誰か助けてもらえますか?

4

2 に答える 2

1

あなたがやろうとしていることはdidInsertElement、すべてのビューでフックを定義することで実現でき、DOM 要素の ID を取得した後、必要な jQuery コードを実行できます。これを「すべて」のビューで実行したいので、コードを実行する必要があるすべてのビューが拡張される一般的なビューを作成できます。

例:

App.GeneralView = Ember.View.extend({
  didInsertElement: function() {
    var width = 0; 
    $('.photos article').each(function(){ 
      width = width + $(this).width();
    });
    $('.photos').width(width);
  }
});

App.IndexView = App.GeneralView.extend();
...

でアクセスできるビューのIDを取得する必要がある場合はthis.get('elementId')、jsbinも変更しました。動作中のバージョンについては、こちらを参照してください。

それが役に立てば幸い。

于 2013-06-29T00:03:18.200 に答える
0

これで修正:

App.GeneralView = Ember.View.extend({
    didInsertElement: function() {
        if (this.$().find(".post img").length > 0) {
            var WIDTH = 0, HEIGHT = $(window).height(), SIDEBAR_WIDTH =   $('#sidebar').outerWidth();
            this.$().find(".post").each(function(){
                WIDTH += parseInt($(this).find('img').attr('width'));
                $(this).find('img').height(HEIGHT - 80).removeAttr('width');
            });
            $('body').children('.ember-view').innerWidth(WIDTH);
        } else {
            Ember.run.next(this, function() {
                this.didInsertElement();
            });
        }
    }
});

それが最善のアプローチかどうかはわかりませんが、機能します;s

于 2013-06-30T19:37:10.320 に答える