用(输入)参数在函数中实现类似的函数?

时间:2015-09-21 14:03:53

标签: angularjs ionic-framework

我有以下功能从我的Ionic应用程序启动Skype:

myApp.controller("launchSkypeCtrl", function($scope, $ionicPopup, $cordovaInAppBrowser) {
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {  
        $scope.launchSkype = function launchSkype() {
            var scheme;             
            if (device.platform === 'iOS') {
                scheme = 'skype://';
                } else if (device.platform === 'Android') {
                scheme = 'com.skype.raider';
                } else if (device.platform === 'wp') {
                scheme = 'skype:';
                } else if (device.platform === 'windows8') {
                scheme = 'skype:';
            }

            navigator.startApp.check(scheme, function(message) { /* success */
                navigator.startApp.start(scheme, function(message) {
                    }, function(error) { /* error */
                    $ionicPopup.alert({
                        title: "Skype not startet!",
                        content: "Skype not startet!"
                    })
                });
                }, function(error) {
                $ionicPopup.alert({
                    title: "Skype not installed",
                    content: "Skype not installed on this device. Please install it!"
                })
            });
        }
    }
});

我也有类似的功能,可以启动Google环聊:

myApp.controller("launchHangoutsCtrl", function($scope, $ionicPopup, $cordovaInAppBrowser) {
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {  
        $scope.launchHangouts = function launchSkype() {
            var scheme;

            if (device.platform === 'iOS') {
                scheme = 'gtalk://';
                } else if (device.platform === 'Android') {
                scheme = 'com.google.android.talk';
                } else if (device.platform === 'wp') {
                scheme = 'gtalk:';
                } else if (device.platform === 'windows8') {
                scheme = 'gtalk:';
            }

            navigator.startApp.check(scheme, function(message) { /* success */
                navigator.startApp.start(scheme, function(message) {
                    }, function(error) { /* error */
                    $ionicPopup.alert({
                        title: "Google Hangouts not started!",
                        content: "Google Hangouts not started!"
                    })
                });
                }, function(error) {
                $ionicPopup.alert({
                    title: "Google Hangouts not installed!",
                    content: "Google Hangouts not installed! Please install it!"
                })
            });
        }
    }
});

可以看出,只有应用程序的链接不同。否则一切都是一样的。错误当然与应用名称相关联。我的问题是如何将这两个相同的函数与函数中的不同应用程序链接放在一起?我有两个以上的函数。因此,我将避免重复相同的功能。 如何在单个功能中输出相应应用的错误消息? 包含上述功能的功能如何? 如何调用相应应用的开头看起来像?

1 个答案:

答案 0 :(得分:0)

您可以将相应的字符串作为参数传递。在这种情况下,一个对象:

$scope.launchSykpe

您定义对象的位置取决于您。

修改 要使$scope.launchHangoutsfunction launchApp($ionicPopup, appConfig) { return function() { var scheme; if (device.platform === 'iOS') { scheme = appConfig.iosScheme; } else if (device.platform === 'Android') { scheme = appConfig.androidScheme; } else if (device.platform === 'wp') { scheme = appConfig.wpScheme; } else if (device.platform === 'windows8') { scheme = appConfig.windows8Scheme; } navigator.startApp.check(scheme, function(message) { /* success */ navigator.startApp.start(scheme, function(message) { }, function(error) { /* error */ $ionicPopup.alert({ title: appConfig.appName + " not started!", content: appConfig.appName + " not started!" }) }); }, function(error) { $ionicPopup.alert({ title: appConfig.appName + " not installed!", content: appConfig.appName + " not installed! Please install it!" }) }); }; } 成为函数而不是调用,请使用以下launchApp函数:

createMessageNotification = function(message) {
  var messager = ChatRooms.find( {}, { chatIds: { $slice: 0,1 } } );
  //console.log(messager);

  if(messager !== Meteor.userId()){
    Notifications.insert({

      message: message.value
    });
  }
}

我没有对它进行测试,但现在应该按照惯例进行测试。