javascript 角度js模型关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14530251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angular js model relationships
提问by Devin Crossman
I've been trying out Angular JS for the past couple of days and one thing I can't figure out is how to work with relationships between models.
过去几天我一直在尝试 Angular JS,但我无法弄清楚的一件事是如何处理模型之间的关系。
The project I'm working on has a Users model and an Accounts model. I have it set up on my database that each Account has a field called 'ownedBy' which is a foreign key reference to the id of the user that owns that account.
我正在处理的项目有一个用户模型和一个帐户模型。我在我的数据库中设置了每个帐户都有一个名为“ownedBy”的字段,它是对拥有该帐户的用户 ID 的外键引用。
In Angular I have the following set up in a file called main.js
在 Angular 中,我在名为 main.js 的文件中设置了以下内容
var myApp = angular.module('myApp', ['ngResource']);
var Users = myApp.factory('Users', function($resource) {
var User = $resource('http://api.mydomain.ca/users/:id',
{id:'@id'},
{});
return User;
});
var Accounts = myApp.factory('Accounts', function($resource) {
var Accounts = $resource('http://api.mydomain.ca/accounts/:id',
{id:'@id'},
{});
return Accounts;
});
function UsersCtrl($scope, Users) {
$scope.users = Users.query();
}
function AccountsCtrl($scope, Accounts) {
$scope.accounts = Accounts.query();
}
and the following template
和以下模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Angular Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css?v=2.2.1">
</head>
<body>
<div ng-app="myApp">
<div ng-controller="UsersCtrl">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>{{user.id}}</td>
<td>{{user.firstName}}</td>
<td>{{user.lastName}}</td>
</tr>
</tbody>
</table>
</div>
<div ng-controller="AccountsCtrl">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Owned By</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="account in accounts">
<td>{{account.id}}</td>
<td>{{account.ownedBy}}</td>
</tr>
</tbody>
</table>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular-resource.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js?v=2.2.1"></script>
<script src="js/main.js"></script>
</body>
</html>
and this is working. It pulls a JSON resource from my REST server and displays it in a table. What's the next step I need to take to end up with one table that shows users and their account numbers? (the equivalent of a database JOIN?) Is there a different way to do it for a one to many relationship? (ie... an account has many transactions)
这是有效的。它从我的 REST 服务器中提取一个 JSON 资源并将其显示在表格中。为了得到一个显示用户及其帐号的表格,我需要采取的下一步是什么?(相当于数据库 JOIN?)对于一对多关系,是否有不同的方法?(即...一个账户有很多交易)
Thanks for the help :)
谢谢您的帮助 :)
采纳答案by Josh David Miller
$resource
doesn't contain any way to deal with relationships that aren't handled by the server, but it's pretty simply with $http
:
$resource
不包含任何处理服务器未处理的关系的方法,但它非常简单$http
:
module.factory( 'UserService', function ( $http, $q ) {
return {
get: function getUser( id ) {
// We create our own promise to return
var deferred = $q.defer();
$http.get('/users/'+id).then( function ( user ) {
$http.get('/accounts/'+user.id).then( function ( acct ) {
// Add the account info however you want
user.account = acct;
// resolve the promise
deferred.resolve( user );
}, function getAcctError() { deferred.reject(); } );
}, function getUserError() { deferred.reject(); } );
return deferred.promise;
}
};
});
And then in your controller, you can just use it like any other promise:
然后在你的控制器中,你可以像任何其他承诺一样使用它:
UserService.get( $scope.userId ).then( function ( user ) {
$scope.user = user;
});
And it's available for your template!
它可用于您的模板!
<div>
User: "{{user.firstName}} {{user.lastName}}" with Acct ID "{{user.acct.id}}".
</div>
回答by Tyrone Wilson
I use js-dataif I need relationships in the UI. The library handles relationships and data modelling pretty elegantly in general. I have found it easier to use even if you are just looking for a nice API interface. I prefer it to ngResource.
如果我需要 UI 中的关系,我会使用js-data。该库通常非常优雅地处理关系和数据建模。我发现即使您只是在寻找一个不错的 API 接口,它也更易于使用。与 ngResource 相比,我更喜欢它。
In your case you would have a model for User and model for Account
在您的情况下,您将拥有用户模型和帐户模型
src/app/data/account.model.coffee
src/app/data/account.model.coffee
angular.module 'app.data' #this can be your module name
.factory 'Account', (DS) ->
DS.defineResource
name: 'account'
endpoint: 'accounts'
relations:
belongsTo:
user:
localKey: 'userId'
localField: 'user'
src/app/data/user.model.coffee
src/app/data/user.model.coffee
angular.module 'app.data'
.factory 'User', (DS) ->
DS.defineResource
name: 'user'
endpoint: 'users'
relations:
belongsTo:
account: #make sure this matches the 'name' property of the other model
foreignKey: 'userId'
localField: 'account'