将我的Backbone模型信息传递给Cordova相机插件的最佳方法

时间:2014-04-03 14:48:45

标签: javascript ios backbone.js cordova marionette

我正在尝试使用Cordova + Backbone + Backbone.Marionette创建一个移动应用程序屏幕,该屏幕将有一个摄像头的占位符图标(我还使用require.js等等,但不要&# 39;我认为这与我遇到的问题有关。当点击占位符时,它会调出本机设备相机应用程序,然后用户可以拍照,占位符图标将替换为他们拍摄的图片。

我的问题是,一旦我真正达到拍摄照片的地步,用新照片更新骨干模型的最佳方法是什么?在Cordova相机插件的成功功能(我的代码中的cameraSuccess)中使用图片数据更新模型是有意义的。但是如何将我的模型传递给成功函数呢?我知道我可以使用某种全局变量来实现这一点,但必须有更好的方法!

从下面的代码片段可以看出,当我的视图触发事件拍照(图片:拍摄)时,我可以通过传递的args对象访问我的模型。但在我可以使用这个args对象之前,我需要调用相机API,确保它成功,然后将结果输出放入我的模型中。

require(['app','views/cameraview','models/camera'],function(Application,cameraview,cammod){

document.addEventListener("deviceready",onDeviceReady, false);

function onDeviceReady()
{
    //start the Marionette application
    Application.start();

    //create a new picture model with the placeholder picture
    var cameramod = new cammod({ picture:"res/placeholder.png" });

    //create a new view, this is basically just using an underscore template to shove the model's picture into an img tag.
    var CameraView = new cameraview({model:cameramod});

    Application.cameraregion.show(CameraView);

    //When the placeholder img is tapped, the view triggers the picture:taken event
    CameraView.on("picture:taken", function(args){

    //**** here I have access to the model with args.model, but I don't know how to properly access that from cameraSuccess ****

    navigator.camera.getPicture(cameraSuccess, cameraError, { 
        quality: 50,
        destinationType: Camera.DestinationType.DATA_URL,
        allowEdit: true,
        saveToPhotoAlbum: true
        });
    });
}

//Camera API success callback function
function cameraSuccess(imageData){

    //*** I need to set the model's picture as shown below, but have no access to the model ****
    //args.model.set({picture:"data:image/jpeg;base64," + imageData});

}
function cameraError(message){

    setTimeout(function() {
        alert('Failed because: ' + message);
    }, 0);

}
});

我认为我的问题很简单,我的应用程序逻辑代码块应该足够了。但是,如果需要,我还可以包括我的视图,模型,模板,应用程序或其他任何内容。

1 个答案:

答案 0 :(得分:1)

除了显而易见的清理之外,如果你在onDeviceReady函数中定义了cameraSuccess函数,它就可以访问有问题的模型,因为它将成为闭包的一部分