如何延长一个AngularJS资源($资源)的构造函数? [英] How can I extend the constructor of an AngularJS resource ($resource)?

查看:156
本文介绍了如何延长一个AngularJS资源($资源)的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,使用 $资源定义,我成功加载。

I have a model, defined using $resource, that I am successfully loading.

每个加载实例,如许,我所​​定义的类的实例。

Each loaded instance is, as promised, an instance of the class I defined.

(下面的例子是从角文档。在这里面, User.get 导致一个对象,它是一个的instanceof用户

(The example below is from the Angular docs. In it, User.get results in an object that is an instanceof User.)

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

不过,想象每个用户过来这样的线:

However, imagine each User comes over the wire like this:

{
  "username": "Bob",
  "preferences": [
    {
      "id": 1,
      "title": "foo",
      "value": false
    }
  ] 
}

我定义了一个 preference 工厂,增加了有价值的方法,以 preference 的对象。但是,当用户负载,这些 preferences 不是 preference S,自然。

I defined a Preference factory that adds valuable methods to Preference objects. But when a User loads, those preferences aren’t Preferences, naturally.

我试图这样的:

User.prototype.constructor = function(obj) {
  _.extend(this, obj);
  this.items = _.map(this.preferences, function(pref) {
    return new Preference(pref);
  });
  console.log('Our constructor ran'); // never logs anything
}

但它没有任何效果,从来没有记录任何东西。

But it has no effect and never logs anything.

我怎样才能让每一个项目在我的用户 S' preferences 阵列<实例code> preference ?

How can I make each item in my Users’ preferences array an instance of Preference?

推荐答案

$资源是一个简单的实现,并在事情没有这样的。

$resource is a simple implementation, and lacks in things like this.

User.prototype.constructor 不会做任何事情;角不会尝试像它的面向对象的,不像其他库。这只是JavaScript的。

User.prototype.constructor won't do anything; angular doesn't try to act like it's object oriented, unlike other libraries. It's just javascript.

..但幸运的是,你必须承诺和JavaScript :-)。这里有一个方法,你可以做到这一点:

..But luckily, you have promises and javascript :-). Here's a way you could do it:

function wrapPreferences(user) {
  user.preferences = _.map(user.preferences, function(p) {
    return new Preference(p);
  });
  return user;
}

var get = User.get;
User.get = function() {
  return get.apply(User, arguments).$then(wrapPreferences);
};
var $get = User.prototype.$get;
User.prototype.$get = function() {
  return $get.apply(this, arguments).$then(wrapPreferences);
};

您可以抽象成装饰任何资源的方法,一种方法是:它需要一个对象,方法名称的数组和装饰功能

You could abstract this into a method which decorates any of a resource's methods: It takes an object, an array of method names, and a decorator function.

function decorateResource(Resource, methodNames, decorator) {
  _.forEach(methodNames, function(methodName) {
    var method = Resource[methodName];
    Resource[methodName] = function() {
      return method.apply(Resource, arguments).$then(decorator);
    };
    var $method = Resource.prototype[methodName];
    Resource.prototype[methodName] = function() {
      return $method.apply(this, arguments).$then(decorator);
    };
  });
}
decorateResource(User, ['get', 'query'], wrapPreferences);

这篇关于如何延长一个AngularJS资源($资源)的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆