如何使用业务逻辑扩展已解析的$资源
How to extend a resolved $resource with business logic
我有以下场景
服务:
.factory(
'itemsApi',
function($resource) {
return $resource("/api/items/:id", { id: "@_id" }, {
'create': { method: 'POST' },
'get': { method: 'GET', isArray: false },
'update': { method: 'PUT' }
});
})
路由器
$routeProvider
.when("/item/:id", {
templateUrl: "item.tpl.html",
controller: "ItemController",
resolve: {
item: ['$route', 'itemsApi', function ( $route, itemsApi) {
return itemsApi.get({ id: $route.current.params.id });
}]
}
}
})
控制器
.controller('ItemController',['$scope','item', function($scope, item) {
$scope.item = item;
}
到目前为止一切都很好。我的控制器里有这个项目。现在我想用我的业务逻辑来扩展$scope.item
。
业务逻辑
function Item() {}
Item.prototype.totalPrice = function() {
// $scope.item should be here 'this'
return this.price + this.shipping;
}
目标:
以Angular方式用业务逻辑扩展$scope.item
。
我试过使用angular.extend
,但不起作用$scope.item没有totalPrice函数。
例如:控制器中的
$scope.item = angular.extend(item, new Item());
问题
如何优雅地得到$scope.item
包含Item
类的方法?
编辑
@ryeballar提出的解决方案产生了丑陋的效果。
现在"$scope.item"将$resource包装在"$scoper.item.item"中
所以我应该更改文档中的每个绑定路径。
发件人
<span ng-bind="item.price"></span>
收件人
<span ng-bind="item.item.price"></span>
这对我来说是不可接受的,感觉很糟糕。然后我想到了这个解决方案
项构造函数
var Item = function Item(item) {
// mixing
angular.extend(this, item);
};
这解决了上一个问题,但产生了新的问题。
现在$scope.item
不包含$resource
方法。(例如$scope.item.update()
)由于__proto__
从Resourse
更改为Item
最后
虽然CCD_ 12不再具有CCD_ 13和CCD_。则可以使用CCD_ 15。
if ($scope.item._id) {
itemsApi.update({ id: $scope.item._id}, $scope.item)
.$promise.then(function(item) {
$scope.item = new Indoor(item);
});
} else {
itemsApi.create($scope.item)
.$promise.then(function(item) {
$scope.item= new Indoor(item);
});
}
为Item
模型创建一个工厂服务,并将其注入解析中,然后将解析后的值作为Item
对象返回。
项目服务
.factory('Item', function() {
var Item = function(item) {
this.item = item;
};
Item.prototype.totalPrice = function() {
return this.item.price + this.item.shipping;
};
return Item;
});
路由器
$routeProvider
.when("/item/:id", {
templateUrl: "item.tpl.html",
controller: "ItemController",
resolve: {
item: ['$route', 'itemsApi', 'Item', function ( $route, itemsApi, Item) {
return itemsApi.get({ id: $route.current.params.id })
.$promise.then(function(item) {
return new Item(item);
});
}]
}
}
});
控制器
.controller('ItemController', ['$scope', 'item', function($scope, item) {
$scope.item = item; // this is an Item Object
}]);
HTML
{{item.totalPrice()}}
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 扩展移相器按钮类不工作
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- TableExport jquery插件:文件名和扩展名问题
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 扩展SVGTextElement时出现Typescript Uncaught TypeError
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- Javascript”;类“;带有参数的扩展
- 内容安全策略指令:;脚本src'self'blob:文件系统:chrome扩展资源:“;获取是否时
- 在扩展 jQuery 之前加载外部资源
- 重定向到火狐中的扩展资源
- Chrome 扩展程序开发错误:资源必须在web_accessible_resources中列出
- 下载网页的HTTPS资源,用于谷歌浏览器扩展程序
- 如何访问firefox扩展资源
- 如何使用业务逻辑扩展已解析的$资源
- 为什么JSF以文件扩展名. JSF和GET参数ln=js交付JavaScript资源?
- 资源的扩展名与其实际内容不一致