Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run function after view finish render change [duplicate]

Tags:

ember.js

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/

like image 775
Bank Avatar asked Dec 01 '25 10:12

Bank


2 Answers

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

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

like image 119
Zeus Avatar answered Dec 03 '25 01:12

Zeus


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.

like image 20
Wilker Lucio Avatar answered Dec 03 '25 01:12

Wilker Lucio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!