12

私はBackbone.jsの実験を開始し、Backbone.Modelのurlプロパティのドキュメントのドキュメントに感銘を受けました。

特に、HATEOAS/ハイパーメディアを使用してクライアントを駆動するRESTAPIを構築しています。

コレクション内のアイテムのURL自体を構築するBackboneのデフォルトの動作の有用性を確認できますが、私の場合は、解析されたデータからモデルURLを構築することをお勧めします。

これを行うためにBackboneを拡張/構築した人はいますか?たぶん、 HALのような「標準」に基づいて構築されていますか?

編集:

明確にするために、私が次のものを持っているとしましょう:

GET /orders >>

[
  {
     "_links": {
       "self": "/orders/123"
     }
     "name": "Order #123",
     "date": "2012/02/23"
  },
  {
     "_links": {
       "self": "/orders/6666"
     }
     "name": "Order #666",
     "date": "2012/03/01"
  },
]

そして私は次のような注文モデルを持っています:

var Order = Backbone.Model.extend({
});

urlHALの「自己」参照からプロパティを自動的に引き出したいのですが。(テストされていない)次のような新しいベースモデルを作成すると思います。

var HalModel = Backbone.Model.extend({
  url: function() {
    return get("_links").self;
  },
});

考え?

4

4 に答える 4

10

私はこれを正確に行うためにバックボーンを拡張しました。ライブラリはここから入手できます。

https://github.com/mikekelly/backbone.hal

于 2012-05-30T21:56:24.197 に答える
4

@Peteの説明に感謝します。

私はあなたの提案が何であるかを理解していると思います、そして私はそれがうまくいくと思います。ただし、あなたの例では/Orders、注文を取得する前に、まずURLを知っている必要がありました。また、jsonを作り直してidプロパティを作成すると、バックボーンのデフォルトの実装にかなり近くなります。

ここで、汎用モデルまたは基本モデル(たとえばHALModel)を使用し、それをデータでブートストラップするだけの場合、このアプローチは有用であり、間違いなく機能する可能性があります。ただし、解析をオーバーライドしてURLを引き出し、モデルに設定することを検討します。

parse: function(response) {
    this.url = response._links.self;
    delete response._links;
    return response;
}
于 2012-03-02T02:54:36.203 に答える
2

ここで、 gomoob / backbone.hateoasを使用して簡単に行う方法を説明するために、Simonの応答を補足します。

// Instanciation of an Hal.Model object is done the same way as you're 
// used to with a standard Backbone model
var user = new Hal.Model({
    firstName: "John",
    lastName: "Doe",
    _links: {
        avatar: {
            href: "http://localhost/api/users/1/avatar.png" 
        },
        self: {
            href: "http://localhost/api/users/1"
        }
    },
    _embedded: {
        address: {
            "city" : "Paris",
            "country" : "France",
            "street" : "142 Rue de Rivoli",
            "zip" : "75001",
            "_links" : {
                "self" : {
                    "href" : "http://localhost/api/addresses/1"
                }
            }
        }
    }
});

// Now we you can easily get links, those lines are equivalent
var link1 = user.getLink('avatar');
var link2 = user.getLinks().get('avatar'); 

// So getting self link is simple too
var self = user.getLink('self');

// All the Hal.Link objects returned by backbone.hateoas are in fact 
// standard Backbone models so its standard Backbone
link1.get('href');
link1.getHref();

// You can do more with shortcut methods if your HAL links 
// have more properties
link1.get('deprecation');
link1.getDeprecation();
link1.get('name');
link1.getName();
link1.get('hreflang');
link1.getHreflang();
link1.get('profile');
link1.getProfile();
link1.get('title');
link1.getTitle();
link1.get('type');
link1.getType();
linke1.get('templated');
link1.isTemplated();

// You can also manipulate embedded resources if you need
user.getEmbedded('address').get('city');
user.getEmbedded('address').getLink('self');
...

最後に、Hal.Model.url()実装を提供します。これは、標準のBackbone url()よりも強力で、HALを使用する場合に非常に便利です。

// By default url() returns the href of the self link if this self 
// link is present
user.url(); 

// If the self link is not defined then url() has the same behavior 
// as standard Backbone url() method
// My user is link to a user collection having a URL equal to 
// 'http://localhost/user1'
user.url(); // http://localhost/users/1

// My user is not link to a user collection in this case the URL is 
// generate using the model urlRoot property by default
user.urlRoot = 'http://myserver/users';
user.url(); // http://localhost/users/1

// backbone.hateoas also allows you to define an application wide root 
// URL which prevent to use absolute URLs everywhere in your code
Hal.urlRoot = 'http://localhost/api';  // HAL root API URL

var user = new Hal.Model({ id : 1});
user.urlMiddle = 'users'; 
user.url(); // http://localhost/api/users/1

これがお役に立てば幸いです。サポートが必要な場合は、遠慮なくgithubに問題を投稿してください。

于 2015-07-02T05:36:28.957 に答える
1

モデルのurl関数をオーバーライドして、必要に応じてURLを計算できます。それは完全に拡張可能です。

于 2012-03-01T17:41:28.603 に答える