Re-use the Phonegap Cordova mobile app code for web application

落爺英雄遲暮 提交于 2019-12-02 19:04:21

问题


We have a mobile app developed with Ionic and Rails as API. Now we are planning to reuse the PhoneGap code along with Ionic tags for the web application. I'm still not completely sure whether it's possible or not. Has someone tried this? I see that placing the Ionic code (which comes inside the www/) in Lamp server makes it work to open in the browser (which I tried locally). Is that correct way? Can someone suggest me a better way for this. A guide or links for the same is also much appreciated.


回答1:


Yes, I've developed some hybrid apps based on Ionic Framework and also corresponding web apps reusing up to 90% of codebase.

The webapp projects share almost all the Angular modules, in particular services and directives, with hybrid app projects. Some functionalities and mobile-specific features are wrapped in a module different for hybridand for webapp projects.

For example I have a wrapper.ionic.js used in hybrid (Ionic) projects which contains for example this factory:

angular.module('components.wrapper', [])
.factory('$myPopup', ['$ionicPopup', function($ionicPopup) {
    var scope = {};

    scope.alert = function (params) {
        return $ionicPopup.alert(params);
    }

    scope.show = function (params) {
        return $ionicPopup.show(params);
    }

    scope.confirm = function (params) {
        return $ionicPopup.confirm(params);
    }

    return scope;
}])
...

The counterpart for webapp projects is wrapper.bootstrap.js (based on https://angular-ui.github.io):

angular.module('components.wrapper', [])
.factory('$myPopup', ['$modal', function($modal) {
    var scope = {};

    scope.animation = true;
    scope.size = 'sm';          // values: '', 'lg', 'sm'

    scope.alert = function (params) {
        var alert = $modal.open({
            animation: scope.animation,
            size: scope.size,
            backdrop: true,
            template:
              '<div class="modal-header"><h4 class="modal-title '+params.cssClass+'">'+params.title+'</h4></div>' +
              '<div class="modal-body '+params.cssClass+'">' +params.template + '</div>' +
              '<div class="modal-footer"><button class="button button-positive" type="button" ng-click="cancel()">Ok</button>' +
              '</div>',
            controller: ['$scope', '$modalInstance', function ($scope, $modalInstance) {
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            }],
            controllerAs: 'ctrl'
        });
        return alert;
    }
...

So you can use both in hybrid and webapp the service $myPopup.

Regarding HTML (index and view templates) the situation is more complex. Many of the Ionic tags (directives and CSS) are mobile friendly but not optimize for webapps which can be seen from desktops. Here I have used both Ionic tags and UI Boostrap, but with preference for the second one.



来源:https://stackoverflow.com/questions/34282230/re-use-the-phonegap-cordova-mobile-app-code-for-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!