2015-08-01 3 views
0

Я пытаюсь добавить PhoneGap imagePicker плагин для моего приложения, и я получаю следующее сообщение об ошибке при попытке вызова функции:PhoneGap плагин - imagePicker - Получение сообщения об ошибке

"Uncaught TypeError: Cannot read property 'getPictures' of undefined", source: file:///android_asset/www/js/main.js (85)

Я установил плагин с помощью CLI в соответствии с инструкциями. Я скопировал .js в соответствующую папку и ссылался на нее.

Вот соответствующие разделы на разных страницах:

Html:

<!DOCTYPE html> 

<html> 
    <head> 
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"> 
    <meta charset="utf-8" /> 
    <meta name="format-detection" content="telephone=no" /> 
    <meta name="msapplication-tap-highlight" content="no" /> 
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height" /> 

    <link rel="stylesheet" type="text/css" href="css/index.css" /> 
    <link href="jquerymobile/jquery.mobile-1.4.5.css" rel="stylesheet" type="text/css" /> 

    <script src="js/jquery.js" type="text/javascript"></script> 
    <script src="jquerymobile/jquery.mobile-1.4.5.min.js" type="text/javascript"></script> 
    <script src="js/imagepicker.js" type="text/javascript"></script> 
    <script src="js/main.js"></script> 
    </head> 
    <body> 
    <!-- a bunch of irrelevant stuff --> 
    <a href="#" data-role="button" data-inline="true" data-icon="arrow-u" data-iconpos="bottom" id="btn_upload">Upload</a> 
    </body> 
</html> 

main.js:

$(document).on("pageshow", "#thepageinquestion", function() { 
    $("#btn_upload").click(function() { 
     window.imagePicker.getPictures(// <--- this is line 85 
      function(results) { 
       for (var i = 0; i < results.length; i++) { 
        console.log('Image URI: ' + results[i]); 
       } 
      }, function (error) { 
       console.log('Error: ' + error); 
      } 
     ); 
    }); 
}); 

imagepicker.js:

cordova.define("com.synconset.imagepicker.ImagePicker", function(require, exports, module) { /*global cordova,window,console*/ 
/** 
* An Image Picker plugin for Cordova 
* 
* Developed by Wymsee for Sync OnSet 
*/ 

var ImagePicker = function() { 

}; 

/* 
* success - success callback 
* fail - error callback 
* options 
*  .maximumImagesCount - max images to be selected, defaults to 15. If this is set to 1, 
*        upon selection of a single image, the plugin will return it. 
*  .width - width to resize image to (if one of height/width is 0, will resize to fit the 
*    other while keeping aspect ratio, if both height and width are 0, the full size 
*    image will be returned) 
*  .height - height to resize image to 
*  .quality - quality of resized image, defaults to 100 
*/ 
ImagePicker.prototype.getPictures = function(success, fail, options) { 
    if (!options) { 
     options = {}; 
    } 

    var params = { 
     maximumImagesCount: options.maximumImagesCount ? options.maximumImagesCount : 15, 
     width: options.width ? options.width : 0, 
     height: options.height ? options.height : 0, 
     quality: options.quality ? options.quality : 100 
    }; 

    return cordova.exec(success, fail, "ImagePicker", "getPictures", [params]); 
}; 

window.imagePicker = new ImagePicker(); 

}); 
+0

По ошибке, '' window.imagePicker' является undefined'. Вы устанавливаете 'imagePicker' как глобальную переменную где угодно, делая' window.imagePicker = ... '? – pushkin

+0

@Pushkin, var определяется в файле imagepicker.js. Я отредактирую вопрос, чтобы включить его. – tllewellyn

+0

Похоже, что файл imagepicker.js почему-то не читается страницей. Независимо от изменений, внесенных в него, ничего не происходит. Даже когда я запускаю страницу с помощью простого предупреждения («привет»); звонок не читается. Мысли? – tllewellyn

ответ

1

После избивая это часами, наконец, понял, что внешний файл javascript не загружается. Ошибка новичков, я разместил ссылку на скрипт в заголовке страницы, а не в тегах data-role = "page".

Найдено ответ здесь:

JQuery Mobile Change Page doesnt load JS files

Смежные вопросы