Ionic 整理项目结构 - 抽离 + 功能模块划分

Ionic 整理项目结构 - 抽离 + 功能模块划分

标签: ionic

ionic start myApp tabs 在官方 tabs 案例模板进行改造

重新整理结构

为什么要抽离

  • controller.js 所有的控制器都在 controller.js 中,就会动不动就几百行上千行,对于查找运维定位都很不方便

  • app.js 所有的路由文件都在这里,那每个合作的人都会修改这个文件,那就会产生大量的冲突,非常不方便,所以要分离

抽取步骤:

  • app.js run 控制项目启动的
  • config.js config 控制不同平台兼容性。ionic 虽然显示一套代码,但是 ionic在不同的平台上显示的效果是不一样的
  • global.js constant 全局变量
    我们会有很多的变量,服务器地址,版本号等等都是不经常改变的,只要一引入对应模块,我们就可以公用这些变量
  • route.js config 控制路由跳转的
    全局路由模块

按照MVC思想,进行功能模块划分:

Controller: 业务逻辑的一些功能
Service(Factory): 和服务器进行数据请求访问
Html页面: 功能界面
Route: 子功能路由 js,控制我们的页面跳转


我们进行抽取

  • config.js 配置模块
// 配置模块, 控制不同平台的兼容性
angular.module( 'config', [] )
    .config( function( $ionicConfigProvider ) { // 引入了 $ionicConfigProvider 服务
        // ionicConfigProvider 服务中 platform 平台下 两种系统 tabs的 定位方式
        $ionicConfigProvider.platform.android.tabs.position( "bottom" );
        $ionicConfigProvider.platform.ios.tabs.position( "bottom" );
    })
  • global.js 全局变量模块
// 全局变量模块
angular.module( 'global', [] )
    .constant( "GlobalVariable", {   // 定义全局恒定不变的变量
        'SERVER_PATH': '127.0.0.1:8080/',  // 服务器地址
        'VERSION': '1.0.1'  // 版本号
    })
  • route.js 全局路由模块
    将 app.js 中内容全选复制过来,删除不需要的内容。
angular.module('route', ['starter.controllers', 'starter.services'])

.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider

    .state('tab', {
    url: '/tab',
    abstract: true,
    templateUrl: 'templates/tabs.html'
  })

  .state('tab.dash', {
    url: '/dash',
    views: {
      'tab-dash': {
        templateUrl: 'templates/tab-dash.html',
        controller: 'DashCtrl' 
        // 写完这个controller之后,会自动帮我们把这个controller作用域加到当前页面里
        // 我们在页面中看不到有加 controller,但是作用域已经加上了
      }
    }
  })

  ...
  • app.js 引入依赖,去掉原来的路由相关代码
angular.module('starter', ['ionic','config', 'global', 'route'])

.run(function($ionicPlatform) {
  // 平台自检
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})
  • index.html 全局引入
    <!-- 全局 js 文件 -->
    <script src="js/app.js"></script>
    <script src="js/route.js"></script>
    <script src='js/global.js'></script>
    <script src="js/config.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script> 

功能模块 js 的整理步骤

  • 创建四个文件

    Controller: 业务逻辑的一些功能
    Route: 子功能路由 js,控制我们的页面跳转
    Service文件: 和服务器进行数据请求访问
    Html页面: 功能界面


MVC结构分层思想,是为了降低代码耦合度。 以引导页为例
  • guidePage.html
    <!-- 引导页面 -->
    <ion-view view-title="引导页">
    我是引导页面

    </ion-view>
  • guidePage_controller.js
// guidePage 引导页业务逻辑
// 页面功能: 引导页功能         创建日期: 2017.1.4
// 创建者: Jepson              修改日期:2017.1.4

// 创建模块 guidePage.controller, 引入依赖 guidePage.service
// 加上 点 表示规范,知道从哪来的
// 公司里面 GPT.TeamA.guidePage.controller  GPT公司团队A的...
angular.module( 'guidePage.controller', ['guidePage.service'] )

    // 约定控制器 首字母大写,表示规范
    .controller( 'GuidePageCtrl', [ '$scope', '$state', function( $scope, $state ) {

    }])
  • guidePage_service.js
// guidePage 引导页页面请求模块
angular.module( 'guidePage.service', [] )

    .factory( 'GuidePageFty', function() {

        return null;

    })
  • guidePage_route.js
// guidePage页面子路由

// 创建路由模块,引入控制器模块
angular.module( 'guidePage.route', ['guidePage.controller'] )

    // 这里用的 UI Router 要注入两个服务 $stateProvider $urlRouterProvider
    .config( function( $stateProvider, $urlRouterProvider ) {
        $stateProvider
            .state( 'guidePage', {
                url: '/guidePage',
                templateUrl: 'areas/guidePage/guidePage.html',
                controller: 'GuidePageCtrl'
            })
    })
  • index.html 引入
    <!-- 功能模块 js -->
    <script src="areas/guidePage/guidePage_controller.js"></script>
    <script src="areas/guidePage/guidePage_route.js"></script>
    <script src="areas/guidePage/guidePage_service.js"></script>
  • route.js 我们这里guidePage只是子路由,所以还需要在总路由上引入一下
// 下面的 constroller 控制器就是从 starter.controllers服务来的,所以要引入
angular.module('route', [
  'guidePage.route',   // 引入引导页 子路由
  'starter.controllers', 
  'starter.services'
  ])
  ...
  • 启动 …/#/guidePage 可以看到引导页了!

  • 这里 index.html 的内容要注释掉下面这部分,就可以看见 ‘我是引导页面’ 这些字了

    <!--<ion-nav-bar class="bar-stable">
      <ion-nav-back-button>
      </ion-nav-back-button>
    </ion-nav-bar>-->
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现了首页商品分类,广告栏,分类菜单 <ion-view view-title="阿哥汇商城" hide-nav-bar="true"> <ion-header-bar align-title="left" class="bar-balanced"> <div class="buttons"> <!--ui-sref="category"--> <div class="button icon-left ion-navicon button-clear" ng-click="toggleCategory()"></div> <!--<div class="button icon-left ion-navicon button-clear" ng-click="modal.show()"></div>--> </div> <label class="item-input-wrapper" ui-sref="search" style="height: 33px;"> <i class="icon ion-ios-search placeholder-icon" style="margin: 20px 0px;color: #BBBBBB"></i> <input class="small-text" type="search" ng-model="searchStr" style="background: transparent;width: 100%" placeholder="请输入商品名称"> </label> <div class="buttons"> <div class="button icon-right ion-ios-cart-outline button-clear" ng-click="toCart()"></div> <i class="badge icon-right assertive" style="height:15px;font-size: 10px;margin-left: -10px;top: -8px;padding:0px 5px;float: inherit;background: #FFFFFF" ng-show="true">{{cartSize}}</i> </div> </ion-header-bar> <ion-content scroll="false" ng-show="isCategory" style="height:100%;background-color: rgba(0, 0, 0, 0.50);z-index: 900"> <div style="height: 85%;margin-bottom: 50px;"> <div class="row" style="width: 100%;height:100%;margin: 0;padding: 0;"> <div class="col-50" style="padding: 0;height: 100%;"> <div class="list" style="padding: 0"> <div class="item item-icon-right item-icon-left" ng-repeat="item in categoryAndProducts" ng-click="selectItem(item, $index)" ng-class="{active: activeItem == item}"> <i class="icon balanced" ng-class="iconArr[$index]"></i> {{item.cat_name}} <i class="icon ion-ios-arrow-right balanced" style="font-size: 24px;align-items:center"></i> </div> </div> </div> <div class="col-50" style="padding: 0;height: 100%;"> <div class="list" style="padding: 0;background: #dedede"> <div class="item active-item item-button-right" ng-repeat="cat in subCategory" ng-click="selectCategory(cat.cat_id)" style="background: #dedede"> {{cat.cat_name}} <button class="button button-clear">{{cat.goods_total}}ddd</button> </div> </div> </div> </div> </div> </ion-content> <ion-content class="false" overflow-scroll="true" style="overflow: hidden;"> <!-- <ion-refresher> 下拉刷新指令 --> <ion-refresher pulling-text="下拉刷新" on-refresh="doRefresh()" spinner="android"></ion-refresher> <ion-slide-box active-slide="myActiveSlide" auto-play="true" does-continue="true" show-pager="true" slide-interval="2000" ng-if="ads" style="height: 108px;"> <ion-slide ng-repeat="ad in ads"> <div class="box blue"> <!--<h1>BLUE</h1>--> <img src="{{APPCONFIG.picURL}}/{{ad.src}}" style="width: 100%;height:inherit"> </div> </ion-slide> </ion-slide-box> <ion-list style=""> <ion-item style="padding: 0px;border-color:#FFF"> <!--<h2>{{item.id}}</h2>--> <div style="height: auto; width:100%;padding: 0"> <div class="row row-center" style="height: 40px;width: 100%;padding: 0;font-size: 16px" onclick="save($index)"> <i class="icon" style="width: 4px;height: 50%;background: {{colors[0]}}"></i> <div class="padding" style="width: 100%"><h2 style="font-size: 20px;color:{{colors[0]}}">热门店铺</h2></div> 更多<i class="icon icon-right ion-ios-arrow-right" style="font-size: 24px;margin-right: 20px;margin-left: 3px"></i> </div> <div class="row row-wrap" style="height:auto; width: 100%;padding: 0;margin-top: 0 "> <div class="col col-33 img" ng-repeat="image in hotsuppliers" style="margin: 0px;padding: 7px auto;height: 60px"> <img ng-src="{{APPCONFIG.picURL}}{{image.logo}}" style="height: 45px;width: 90px"/> </div> </div> <div style="background: #F3F3F3;height: 10px;width: 100%"></div> </div> </ion-item> <ion-item collection-repeat="item in categoryAndProducts" style="padding: 0;border-color:#FFF"> <!--<h2>{{item.id}}</h2>--> <div style="height: auto; width:100%;padding: 0"> <div class="row row-center" style="height: 40px;width: 100%;padding: 0;font-size: 16px" ng-click="selectCategory(item.cat_id)"> <i class="icon" style="width: 4px;height: 50%;background: {{colors[(item.cat_id%5)]}}"></i> <div class="padding" style="width: 100%"><h2 style="color: {{colors[(item.cat_id%5)]}};font-size: 20px"> {{item.cat_name}}</h2></div> 更多<i class="icon icon-left ion-ios-arrow-right" style="font-size: 24px;margin-right: 20px;margin-left: 3px"></i> </div> <div class="row row-center" style="padding: 0;margin-top: 0"> <div class="col-33" ng-repeat="product in item.goods" style="padding: 5px" ng-click="goProductDetail(product.goods_id)"> <div class="img" style="width: 100%"> <img class="item-product-img" ng-src="{{APPCONFIG.picURL}}/{{product.goods_thumb}}" style="width: 90px;height: 90px"> <div class="desc" style="text-overflow: ellipsis;white-space:nowrap; overflow:hidden;font-size: 16px;margin-top: 5px"> {{product.goods_name}} </div> <div style="background: #DEDFE0;height: 1px;margin: 2px"></div> <div class="desc assertive" style="text-overflow: ellipsis;overflow:hidden;font-size: 16px"><b>{{product.price | currency:"¥"}}</b></div> </div> </div> </div> <div style="background: #F3F3F3;height: 10px;width: 100%"></div> </div> </ion-item> </ion-list> </ion-content>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值