企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# M层 services/klass.js ``` // 注入klass .controller('KlassEditCtrl', function($scope, config, teacher, $routeParams, klass) { ... // 更新 var update = function (klass, callback) { server.http({ method:'POST', url: '/klass.Update.json', data: klass }, function(response) { callback(response); }); }; ... // Public API here return { // 获取全部教师信息 paginate: function(name, page, pageSize, callback) { return paginate(name, page, pageSize, callback); }, save: save, get: get, update: update }; ... ``` # 单元测试 test/..../klass.js ``` ... // 增加GET的测试 url = config.apiRootPath + '/klass.Get.json?id=1'; $httpBackend.when('GET', url).respond(data); // 增加update测试 url = config.apiRootPath + '/klass.Update.json'; $httpBackend.when('POST', url).respond(data); })); ... // 调用UPDATE klass.update({}, function(){ console.log('klass update 通过'); }); // 模拟数据请求 $httpBackend.flush(); ... ``` ## 控制台 ``` PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 11 of 15 SUCCESS (0 secs / 0.068 secsLOG: 'klass get 通过' PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 11 of 15 SUCCESS (0 secs / 0.068 secsLOG: 'klass update 通过' ``` # C层对接 edit.js ``` // 数据提交 var submit = function() { klass.update($scope.klass, function(response){ if (!angular.equals({}, response.errors)) { // 发生错误 $scope.errors = response.errors; $scope.isError = true; // 发生错误 $scope.message = ''; // 清空消息 } else { // 添加成功 $scope.message = '更新成功'; $scope.isError = false; } }); }; ``` ## 测试: ![https://box.kancloud.cn/223c3311ac3fe57bd870bd64bba7ade2_740x220.gif](https://box.kancloud.cn/223c3311ac3fe57bd870bd64bba7ade2_740x220.gif)