Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用函数时出现主干故障_Javascript_Android_Backbone.js_Cordova - Fatal编程技术网

Javascript 调用函数时出现主干故障

Javascript 调用函数时出现主干故障,javascript,android,backbone.js,cordova,Javascript,Android,Backbone.js,Cordova,我对javascript非常缺乏经验,所以如果这真的很明显,请原谅我 我用这里找到的TodoMVC例子作为科尔多瓦项目的基础。我遇到的问题是将其与Camera和Geolocation插件结合在一起,在Camera/Geolocation回调函数中调用js/views/app-view.js中的任何函数时都会出错。我假设这是一个上下文问题,但我对javascript了解不够,无法解决它 例如,TodoMVC项目使用以下代码行在js\views\app-view.js中添加新项: app.todos

我对javascript非常缺乏经验,所以如果这真的很明显,请原谅我

我用这里找到的TodoMVC例子作为科尔多瓦项目的基础。我遇到的问题是将其与Camera和Geolocation插件结合在一起,在Camera/Geolocation回调函数中调用js/views/app-view.js中的任何函数时都会出错。我假设这是一个上下文问题,但我对javascript了解不够,无法解决它

例如,TodoMVC项目使用以下代码行在js\views\app-view.js中添加新项:

app.todos.create(this.newAttributes());
我正在尝试将此功能与照相机功能结合起来,使用手机的照相机拍摄照片,然后添加一个新的待办事项,并将照片存储在其中。一切正常,除了我尝试在摄影机回调函数中使用上面的代码行外,在底部,如下所示:

onCameraSuccess: function(imageData) {
        // Stores image data in a hidden field to be used later. Not best method but it works
        document.getElementById('imageData').value =  "data:image/jpeg;base64," + imageData;

        // This line should create a new item
        app.todos.create(this.newAttributes());
    },
它给出了错误“Undefined is not a function”,似乎是指“this.newAttributes()”。同样,这似乎是一个上下文问题,因为同一行几乎在同一个脚本中的任何其他地方都适用

OnCameraSuccess是相机的getPicture函数的回调函数:

Camera: function() {
        navigator.camera.getPicture(this.onCameraSuccess, this.onCameraFail, { quality: 50,
            destinationType: Camera.DestinationType.DATA_URL,
            targetWidth: 250,
            targetHeight: 250,
            correctOrientation: true
        });
    },
var _this = this;
navigator.camera.getPicture(
    function(imageData) { _this.onCameraSuccess(imageData) },
    ...
);
类似地,用于地理定位的函数需要两个回调函数,一个是成功函数,另一个是错误函数,但我找不到一种可接受的方法来引用其他函数,这些函数没有给出表示它们不是函数的类型\u MISMATCH\u ERR

onDeviceReady: function() {
        //This line calls the geolocation function and specifies the two callback functions, but it thinks they don't exist
        navigator.geolocation.watchPosition(this.onLocationSuccess, this.onLocationError, {enableHighAccuracy : true});
    },

    onLocationSuccess: function(position) {
        // map logic
    },

    onLocationError: function(error) {
        alert('code: ' +error.code+ '\n' + 'message: ' +error.message + '\n');
    },
它们就在onDeviceReady函数的正下方,但由于某些原因,它找不到它们。this.onLocationSuccess不起作用(即使它引用了所有其他函数),self.onLocationSuccess不起作用。。。同样,这似乎是一个上下文问题,但我无法理解


有什么想法吗?我对javascript的了解还不够,不知道我是否充分解释了这一点,所以也可以随意对我大喊大叫。

javascript中的
的值取决于函数的调用方式。例如,这:

var obj = {
    f: function() { console.log(this) }
};
obj.f();
将在控制台中放置
obj
,但是:

var obj = {
    f: function() { console.log(this) }
};
var f = obj.f;
f();
将(通常)将
窗口
转储到控制台中,即使调用了相同的函数。当人们开始使用JavaScript时,这种
行为几乎让所有人都感到困惑。在您的情况下,文档没有说明
这个
将在
getPicture
回调中是什么,因此
这个
可能是
窗口
而不是您的对象

如果您需要一个带有JavaScript回调函数的特定
this
,通常需要自己安排。一种方法是将所需的
存储在变量中,并使用匿名函数:

Camera: function() {
        navigator.camera.getPicture(this.onCameraSuccess, this.onCameraFail, { quality: 50,
            destinationType: Camera.DestinationType.DATA_URL,
            targetWidth: 250,
            targetHeight: 250,
            correctOrientation: true
        });
    },
var _this = this;
navigator.camera.getPicture(
    function(imageData) { _this.onCameraSuccess(imageData) },
    ...
);
一种更简洁的方法(特别是当函数有参数时)是使用、或几乎每个JavaScript工具包都附带的类似实用程序将函数绑定到所需的
this
。例如,鉴于此:

var obj = {
    f: function() { console.log(this) }
};
var f1 = $.proxy(obj.f, obj);
var f2 = _(obj.f).bind(obj);
var f3 = obj.f.bind(obj);
调用
f1()
f2()
f3()
都将
obj
转储到控制台中。下划线是具有主干的典型工具包,因此这是常见的:

navigator.camera.getPicture(
    _(this.onCameraSuccess).bind(this),
    _(this.onCameraFail).bind(this),
    { ... }
);
下划线还提供了在
initialize
中通常用于将多个函数绑定到位:

initialize: function() {
    _.bindAll(this, 'onCameraSuccess', 'onCameraFail');
    //...
}
然后您可以使用
这个.onCameraSuccess
,而不用担心绑定:

navigator.camera.getPicture(
    this.onCameraSuccess,
    this.onCameraFail,
    { ... }
);

初始化
中使用
可能是主干网中最常见的方法。

由于您将此作为对摄影机函数的引用传递,它将被摄影机上下文和应用程序的当前上下文覆盖

复制当前上下文,如

var currentContext = this;
currentContext.onCameraSuccess,
currentContext.onCameraFail

这适用于应用程序控件从一个上下文移动到另一个上下文的情况,例如:主干视图移动到下划线函数,并移回回调函数等。

哦,对了,onCameraSuccess是相机的getPicture函数的回调函数。我将代码编辑到了原始帖子的中间。所有这些函数都在同一个脚本中。非常有启发性,我现在对“这个”的理解好多了。而且uu.bindAll修复程序工作得很好,相机和地理定位都完全正常。非常感谢你!这行不通<调用函数时,code>currentContext.onCameraSuccess
的行为与此.onCameraSuccess
完全相同。您必须使用匿名函数包装器(
function(x){currentContext.onCameraSuccess(x)}
)才能使
currentContext
有用。