3

Possible Duplicate:
Using Ember.js, how do I run some js after a view is rendered?

I use JWPlayer to show video on my site and JWPlayer use javascript to create viewer.

I have to run JWPlayer script after my hidden view change to show.

jwplayer('video_space').setup({
    'flashplayer': 'player.swf',
    'file': 'http://content.longtailvideo.com/videos/flvplayer.flv',
    'controlbar': 'bottom',
    'width': '470',
    'height': '320'
  });

I binding my view with value on controller

<script type="text/x-handlebars" >
  {{#view Food.previewView }}
    Video session
    <div {{bindAttr class="showPreview"}}>
        <div id='video_space'>Video Loading...</div>
    </div>
    {{#view SC.Button target="Food.appController" action="show"}}
        show
    {{/view}} 
  {{/view}}
</script>

My javascript for all controller and view

Food = Ember.Application.create();

Food.appController = Ember.Object.create({
    showVideo: false,

    show: function(){
        Food.previewView.set('showVideo', true);
    }
});

Food.previewView = Ember.View.extend({
    showVideoBinding: 'Food.appController.showVideo',

    showPreview: function() {
        return this.get('showVideo') ? "show" : "hide";
    }.property('showVideo')
});

If I run JWPlayer script when change "showVideo" variable the video will error because $('#video_space') is still hidden.

Emberjs have some delegate method like "afterViewChange" or "viewDidChange" ?

testing code http://jsfiddle.net/apirak/Ay6Fh/

Community
  • 1
  • 1
Bank
  • 1,783
  • 4
  • 17
  • 26
  • It seems that the canonical solution (using isVisible) is unreliable due to observer ordering. We will be either fixing this or providing an explicit lifecycle callback in the next few days. – Yehuda Katz Dec 18 '11 at 18:24

2 Answers2

6

Override the Ember.View.didInsertElement function and place your customizations in there.

http://docs.emberjs.com/#doc=Ember.View&method=didInsertElement&src=false

Zeus
  • 161
  • 1
  • 3
1

One thing, you did an error when you created your appController, when creating objects you need to use .create instead of .extend, this mistake was causing the button to don't find the "show" action of you controller.

That said, you can still bind on showVideo for you actions, and when it changes you can call your JWPlayer script to show it up.

Wilker Lucio
  • 2,619
  • 2
  • 20
  • 11
  • Thank you for ".create" :-) But when "showVideo" is change, How do I make sure that view(HTML) is completely change. – Bank Dec 19 '11 at 00:58
  • I'm not sure if you can, but, since you are just changing an css property, that html is there anyway, so I think it's safe to just call the JWPlayer to do the job. – Wilker Lucio Dec 19 '11 at 04:53