简体   繁体   English

在现有的JS项目中迷路

[英]Getting lost in an existing JS project

I got into an existing JS project, a sole programmer has been working on it for 6 months and left without much explaining. 我进入了一个现有的JS项目,一个唯一的程序员已经在该项目上工作了6个月,并且没有太多解释。 Anyway it is written in Ionic, and from what I have read and tested, just a tool for building cordova + angular applications. 无论如何,它都是用Ionic编写的,而根据我的阅读和测试,这只是构建cordova + angular应用程序的工具。 Anyway I got the app deployed on my phone fine, cordova command line and even can get a server with Ionic serve, which is very familiar to other node servers. 无论如何,我将应用程序很好地部署在我的电话上,cordova命令行上,甚至可以使用Ionic伺服器获得服务器,这是其他节点服务器非常熟悉的。 My problem is developing. 我的问题正在发展。 I'm used to get the app running on my pc, checking stuff with dev tools, see when and where is a resource being accessed to and more. 我习惯于使该应用程序在我的PC上运行,使用开发工具检查内容,查看何时何地可以访问资源等等。 The app working perfectly normal on my mobile but when getting to the pc I can't get past the login screen with the error "cordova is not defined". 该应用程序在我的手机上运行完全正常,但是进入PC时,我无法通过登录屏幕并显示错误“未定义cordova”。 Checking the code more it is heavily depended on cordova. 更多地检查代码在很大程度上取决于cordova。 What can I do to solve this? 我该怎么解决? Here is a sample code from the project which I run into error on login: 这是项目中的示例代码,我在登录时遇到错误:

var dir = window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir) {
dir.getDirectory("build/", {create:true}, function(aDir) {
    FS.builds = aDir;
}, FS.fsError);
dir.getDirectory("assets/", {create:true}, function(aDir) {
    FS.assets = aDir;
    App.assetURL = FS.assets.toURL();
    document.dispatchEvent(new CustomEvent("FS-ready", {}));
}, FS.fsError);
}, FS.fsError);

Thanks in advance 提前致谢

The mobile application as MVC (Model-View-Controller) contains two parts: 作为MVC (模型-视图-控制器)的移动应用程序包含两个部分:

  • Native code (Android - Java/ Obj-C/Swift - iOS) - Model 本机代码(Android-Java / Obj-C / Swift-iOS)- 模型
  • Ionic(GUI) + Angular + Cordova - View + Controller Ionic(GUI)+ Angular + Cordova- 视图+控制器

By Cordova interfaces GUI can talk with native part. 通过Cordova接口,GUI可以与本机部分通信。

When you try to run www as root in browser (apache or similar), it tells you that cordova is not defined since Browser hasn't cordova sources (it stored in mobile assets). 当您尝试在浏览器(Apache或类似工具)中以root身份运行www ,它会告诉您未定义cordova,因为Browser没有cordova源(它存储在移动资产中)。

Your application starts on mobile when Cordova is ready that doesn't happen in Browser. 当Cordova准备就绪时,您的应用程序将在移动设备上启动,而浏览器中不会发生这种情况。

Anyways to run your project in Browser(Chrome ...) since you use Angular, Controllers talk 1st with Services. 无论如何,由于您使用Angular,因此无论如何在Browser(Chrome ...)中运行项目,Controller都会与Services进行第一对话。 Create some folder data with json files that simulate responses from mobile native to Angular and instead to call cordova API, call by $http.get your json file. 使用json文件创建一些文件夹data ,这些文件data模拟从移动原生到Angular的响应,而要调用cordova API,请通过$http.get json文件。

$ionicPlatform.ready(function() {
  if (ionic.Platform.isAndroid() ) {
     $rootScope.imAndroid = true;
   }
   else if (ionic.Platform.isIOS() ) {
     $rootScope.imiOS = true;
   }
    else{
       $rootScope.imBrowser = true;
    }
} 

in some service: 在某些服务中:

// load JSON data by async way with promise 
app.factory('Items', ['$http',
    function($http) {

        return {
            getJson: function(url) {
                var ItemsJson = $http.get(url).then(function(response) {
                    return response.data;
                });
                return ItemsJson;
            }
        }
    }
]);

So Example looks like: 所以示例看起来像:

self.login = function(){

       if ($rootScope.imBrowser === true) {
             // in Browser we call JSON file
             return Items.getJson('data/loginResponse.json');
        }

       if ( (ionic.Platform.isAndroid() || ionic.Platform.isIOS() )) {  
              // on mobile we call Cordova
              return CordovaService.login();              
         }
   };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM