前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图解AngularJS Wijmo5和LightSwitch

图解AngularJS Wijmo5和LightSwitch

作者头像
葡萄城控件
发布2018-01-10 17:48:26
1.3K0
发布2018-01-10 17:48:26
举报

Visual Studio 2013 中的 LightSwitch 有新增功能,包括更好的团队开发支持以及在构建 HTML 客户端桌面和 Office 365 应用程序方面的改进。本文结合最新发布的Wijmo 5提供的AngularJs进行图解。

image
image

基于Visual Studio LightSwitch作为数据源,我们使用Wijmo 5控件快速的创建 AngularJS应用程序。

image
image

插入数据记录

image
image

业务规则校验

image
image

数据记录更新

image
image

选择数据记录,点击键盘上删除按键

image
image

点击列头,进行数据排序

image
image

并发性校验(由LightSwitch的后端提供)。

Wijmo 5控件集

image
image

2014年10月7日---葡萄城宣布正式发布Wijmo 5。Wijmo 5不再兼容传统浏览器(<= IE9),纯HTML5控件集。并且,发布并Wijmo 5全部的控件中将全面支持Angular JS。

为何使用Wijmo 5和LightSwitch?

  • 为了100%控制UI:LightSwitch HTML Client基于JQuery Mobile,这导致为了控制UI不得不花费大量时间。
  • 为了实现安全性:安全策略一般在Server端实现。无法通过AngularJs前端技术实现。LightSwitch使得安全性非常容易实现。
  • 为了处理并发性:当多人同时更新DB会导致并发性,所幸,LightSwitch已经自动实现该特性。
  • 为了用LightSwitch进行管理界面代码:基于LightSwitch,我们无需用AngularJs实现管理界面代码,LightSwitch已经实现了,故结合LightSwitch和AngularJs使得编程非常容易。
  • 为了加快开发:当前使用LightSwitch,可以大大减少代码编写,这意味着开发进程会加速,bug会减少。

例子如下所示:

image
image

LightSwitch 业务层

image
image

在解决方案视图,在数据源DataSources右键,选择Add Table

image
image

创建ToDo表

image
image

点击写代码按钮,选择Validate方法,在生成的模板中,插入验证代码。

代码语言:javascript
复制
partial void ToDoes_Validate(ToDo entity, EntitySetValidationResultsBuilder results)
        {
            // Do not allow a task to be called {New Task]
            if (entity.TaskName == "[New Task]")
            {
                results.AddEntityError(
                    "Task cannot be named [New Task]"
                    );
            }

            // Do not allow more than 1 incomplete Task
            if (entity.IsComplete == false)
            {
                int intCountOfIncomplete =
                    this.DataWorkspace.ApplicationData.ToDoes
                    .Where(x => x.IsComplete == false)
                    .Where(x => x.Id != entity.Id).Count();

                if (intCountOfIncomplete > 0)
                {
                    results.AddEntityError(
                        "Cannot have more than 1 incomplete Task"
                        );
                }
            }
        }

Wijmo 5代码

Wijmo 5下载地址:输入邮箱即可获得下载URL地址

image
image
image
image

解压Wijmo样例,定位到“..\Samples\JS\Angular\OData”目录,拷贝Vendor和styles文件夹到LightSwitch Server工程的Scripts文件夹。

image
image

创建wijmo.data文件夹,下载ODataCollectionView.js,并拷贝在wijmo.data文件夹下。

AngularJs代码

image
image

在scripts文件夹下创建app.js脚本文件,并输入如下代码。

代码语言:javascript
复制
// 在AngularJS 声明依赖 Wijmo "wj" module:
var app = angular.module('app', ['wj']);

image
image

在scripts文件夹下创建controllers文件夹,并创建appCtrl.js,并输入如下代码。

代码语言:javascript
复制
'use strict';

var app = angular.module('app');

app.controller('appToDoCtrl', function appToDoCtrl($scope) {

    // define data service URL and data types for specific columns
    var oDataService = '/ApplicationData.svc', dataTypes = [];

    // load ToDos table
    $scope.cvToDo = new wijmo.data.ODataCollectionView(
        { serviceUrl: oDataService, entityName: 'ToDoes' },
        ['Id'],
        {
            serverSettings: {
                selects: ['Id', 'RowVersion', 'TaskName',
                    'IsComplete', 'Created', 'Modified']
            }
        },
        dataTypes, loaded, true);

    // Display any errors
    $scope.cvToDo.error.addHandler(function (sender, args) {
        alert(sender.errorMsg);
    });

    // tell scope when tables finish loading
    function loaded() {
        $scope.$apply();
    }
});

创建HTML页面

image
image

创建ToDo.htm页面,并输入如下代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en" ng-app="app" ng-controller="appToDoCtrl">
<head>
    <meta charset="utf-8" />
    <title>To DO</title>
    <!-- ensure IE uses the latest version of IE (yes, yes...) -->
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!-- jQuery/Angular/Bootstrap -->
    <script src="http://code.jquery.com/jquery-2.0.0.min.js" 
            type="text/javascript"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js" 
            type="text/javascript"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular-route.min.js" 
            type="text/javascript"></script>
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js" 
            type="text/javascript"></script>
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" 
          type="text/css" />

    <!-- Wijmo -->
    <script src="scripts/vendor/wijmo.min.js" type="text/javascript"></script>
    <script src="scripts/vendor/wijmo.input.min.js" type="text/javascript"></script>
    <script src="scripts/vendor/wijmo.grid.min.js" type="text/javascript"></script>
    <link href="styles/vendor/wijmo.min.css" rel="stylesheet" />

    <!-- app scripts -->
    <script src="scripts/wijmo.data/ODataCollectionView.js" type="text/javascript"></script>
    <script src="scripts/app.js" type="text/javascript"></script>
    <script src="scripts/controllers/appCtrl.js" type="text/javascript"></script>

    <!-- Wijmo-Angular interop -->
    <script src="scripts/vendor/wijmo.angular.min.js" type="text/javascript"></script>

    <!-- app styles -->
    <link href="styles/app.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div class="header">
        <div class="container" style="position:relative">
            <h1>
                TO DO Example
            </h1>
        </div>
    </div>
</body>
</html>

在<body>内添加Wijmo Grid代码:

代码语言:javascript
复制
<div class="container">
        <wj-flex-grid class="grid" 
                      allow-add-new="true" 
                      allow-delete="true" 
                      items-source="cvToDo">
            <wj-flex-grid-column 
                                 binding="TaskName" 
                                 width="300" 
                                 header="Task Name">
            </wj-flex-grid-column>
            <wj-flex-grid-column 
                                 binding="IsComplete" 
                                 datatype="Boolean" 
                                 width="200" 
                                 header="IsComplete">
            </wj-flex-grid-column>
        </wj-flex-grid>
    </div>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-01-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Wijmo 5控件集
  • 为何使用Wijmo 5和LightSwitch?
  • LightSwitch 业务层
  • Wijmo 5代码
  • AngularJs代码
  • 在scripts文件夹下创建controllers文件夹,并创建appCtrl.js,并输入如下代码。
  • 创建HTML页面
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档