简体   繁体   English

HeadJS加载的脚本仅在第二次单击时运行

[英]Script loaded with HeadJS runs only on second click

Script loaded with HeadJS runs only on second click. HeadJS加载的脚本仅在第二次单击时运行。 How to get Aviary launcher on first click? 如何在首次点击时获得Aviary启动器?

http://jsfiddle.net/ynts/6hgEb/ 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);
}); 

You need to wait for the plugin to initialize, before calling the launch function. 在调用launch函数之前,您需要等待插件初始化。 There is onLoad event you can use: 您可以使用onLoad事件:

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

Answering mikebridge, for the case when it doesn`t open a second time: 回答迈克布里奇(Mikebridge)在第二次没有打开的情况下:

Some dark reason prevents feather to be instantiated more than once. 某些黑暗的原因阻止了羽毛被多次实例化。 Use window.globalAviaryFeather to hold the instance. 使用window.globalAviaryFeather来保存实例。

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)
        }
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM