将元数据添加到AngularJS资源对象

时间:2021-09-21 21:19:03

Suppose I have a resource like the typical one from the AngularJS docs:

假设我有一个类似AngularJS docs中的典型资源:

var User = $resource('/user/:userId', {userId:'@id'});

Is it possible to store some metadata in the object that will survive another request? This is what I want to happen:

是否可以在对象中存储一些元数据,以便在另一个请求中存活?这就是我想要发生的事情:

var bob = User.get({userId: '123'});
// {id: 123, name: 'Bob'}
bob.$foo = 'foo';
// {id: 123, name: 'Bob', $foo: 'foo'}
bob.$get();
// {id: 123, name: 'Bob', $foo: 'foo'}

Notice that the $foo field is still there after the call to $get.

请注意,$ foo字段在调用$ get后仍然存在。

I suspect that this isn't possible. In that case, where is a better place to store it?

我怀疑这是不可能的。在那种情况下,哪里有更好的存放地点?

2 个解决方案

#1


2  

You could make your own get function that retains the metadata:

您可以创建自己的get函数来保留元数据:

User.prototype.getButKeepMetadata = function () {
  var foo = this.$foo;
  var self = this;
  this.$get(function () {
     self.$foo = foo;
  });
}

And instead of bob.$get(), do a bob.getButKeepMetadata().

而不是bob。$ get(),做一个bob.getButKeepMetadata()。

#2


0  

You are correct in thinking it's not possible as you stated it. You could store the result and any updates in an angular constant. That would allow you to have a client side copy in any controller you inject it while still making http requests.

你认为这是不正确的,你说的是正确的。您可以将结果和任何更新存储在角度常量中。这将允许您在仍然发出http请求时注入它的任何控制器中都有客户端副本。

Update with Constant Example

使用常量示例更新

http://jsfiddle.net/jonesmac82/D292w/6/

var myApp = angular.module('myApp', [
'MyCtrlModule',
'myConstant'
]);

var testCtrl = angular.module('MyCtrlModule', []);
testCtrl.controller('firstCtrl', ['$scope', 'user', function ($scope, user) {
    $scope.returnedResource = {
        fullName: 'john doe',
        userId: 123
    };
    user.username = $scope.returnedResource.fullName;
    user.userId = $scope.returnedResource.userId;
}]);

testCtrl.controller('secondCtrl', ['$scope', 'user', function ($scope, user) {
    $scope.newVariable = user.username;
}]);

var testCostant = angular.module('myConstant',[]);
testCostant.constant('user', {
    username: null,
    userId: null
});

Constants are a great way to share info between controllers and can be updated as new http request data comes in.

常量是在控制器之间共享信息的好方法,可以在新的http请求数据进入时更新。

Let me know if that helps.

如果有帮助,请告诉我。

#1


2  

You could make your own get function that retains the metadata:

您可以创建自己的get函数来保留元数据:

User.prototype.getButKeepMetadata = function () {
  var foo = this.$foo;
  var self = this;
  this.$get(function () {
     self.$foo = foo;
  });
}

And instead of bob.$get(), do a bob.getButKeepMetadata().

而不是bob。$ get(),做一个bob.getButKeepMetadata()。

#2


0  

You are correct in thinking it's not possible as you stated it. You could store the result and any updates in an angular constant. That would allow you to have a client side copy in any controller you inject it while still making http requests.

你认为这是不正确的,你说的是正确的。您可以将结果和任何更新存储在角度常量中。这将允许您在仍然发出http请求时注入它的任何控制器中都有客户端副本。

Update with Constant Example

使用常量示例更新

http://jsfiddle.net/jonesmac82/D292w/6/

var myApp = angular.module('myApp', [
'MyCtrlModule',
'myConstant'
]);

var testCtrl = angular.module('MyCtrlModule', []);
testCtrl.controller('firstCtrl', ['$scope', 'user', function ($scope, user) {
    $scope.returnedResource = {
        fullName: 'john doe',
        userId: 123
    };
    user.username = $scope.returnedResource.fullName;
    user.userId = $scope.returnedResource.userId;
}]);

testCtrl.controller('secondCtrl', ['$scope', 'user', function ($scope, user) {
    $scope.newVariable = user.username;
}]);

var testCostant = angular.module('myConstant',[]);
testCostant.constant('user', {
    username: null,
    userId: null
});

Constants are a great way to share info between controllers and can be updated as new http request data comes in.

常量是在控制器之间共享信息的好方法,可以在新的http请求数据进入时更新。

Let me know if that helps.

如果有帮助,请告诉我。