Script loaded with HeadJS runs only on second click

≯℡__Kan透↙ 提交于 2019-12-14 04:07:35

问题


Script loaded with HeadJS runs only on second click. How to get Aviary launcher on first click?

http://jsfiddle.net/ynts/6hgEb/

function aviary(id, src) {
    var $id = id;
    var $src = src;

    head.load(
        "http://feather.aviary.com/js/feather.js",
        function() {

            var featherEditor = new Aviary.Feather({
                apiKey: 12345,
                apiVersion: 3

                // ... ///
            });

            featherEditor.launch({
                image: $id,


                  url: $src
                });
            }
        ); 
}

$(document).on('click', 'img', function(){
    var $img = $(this).attr('src');
    aviary('edit-pic', $img);
}); 

回答1:


You need to wait for the plugin to initialize, before calling the launch function. There is onLoad event you can use:

var featherEditor = new Aviary.Feather({
                apiKey: 12345,
                apiVersion: 3,
                onLoad: function() {
                     featherEditor.launch({
                        image: $id,
                        url: $src
                    });
                }                                    
            }); 



回答2:


Answering mikebridge, for the case when it doesn`t open a second time:

Some dark reason prevents feather to be instantiated more than once. Use window.globalAviaryFeather to hold the instance.

function launch(url) {
    window.globalAviaryFeather.launch({
        image: "imgId",
        url: url
    })
}

var url = "..."

if (window.globalAviaryFeather) {
    launch(url)

} else {
    window.globalAviaryFeather = new Aviary.Feather({
        ...
        onLoad: function() {
            self.launch(url)
        }
    })
}


来源:https://stackoverflow.com/questions/23222589/script-loaded-with-headjs-runs-only-on-second-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!