使用PHP REST API在Durandal上使用BreezeJS(Laravel) [英] BreezeJS on Durandal with PHP REST API (Laravel)

查看:109
本文介绍了使用PHP REST API在Durandal上使用BreezeJS(Laravel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Durandal 2.1编写SPA.到目前为止,一切都很好.但是,在选择正确的数据持久性库以与我的PHP(Laravel)REST API通信时,我遇到了很大的困难.

I'm writing a SPA in Durandal 2.1. So far so good. However, I'm having great difficulties in selecting the right Data Persistence library to communicate with my PHP (Laravel) REST API.

Durandal的第一库是BreezeJS.据说它支持API的没有 oData/ASP.NET实体框架.我已经搜索了很多天,但是找不到任何合适的资源来告诉我如何在基本的PHP REST API中使用BreezeJS.

The number one library for Durandal is BreezeJS. It is said that it supports API's without oData / ASP.NET Entity framework. I've searched for many days now, but I can't find any decent resource telling me how to use BreezeJS with my basic PHP REST API.

愿望清单如下:

  • 在视图之间保留数据
  • 验证模型
  • 状态跟踪(也称为isDirty/cancelChanges)
  • 使用REST API调用获取/发布/放置/删除(对于PUT,仅发送脏内容)

REST API如下.我将使用联系人+地址+国家/地区模型"结构来说明我的API的工作原理.

The REST API is as follows. I'll use a Contact + Address + Country Model structure to explain how my API works.

  • 联系多对多地址
  • 地址属于国家/地区国家/地区有很多地址
  • Contact many-to-many Address
  • Address belongs-to Country a.k.a. Country has-many Address

以下GET请求:

GET /api/v1/contacts
+ Payload:
{
    append_addresses         : 1, // add addresses as nested data
    append_addresses_country : 1, // add country as nested data of addresses
    stack                    : 2, // return no more than 2 contacts (e.g. for pagination)
    page                     : 1, // return the first page (so the first 2 contacts)
    count                    : 1  // return the total number of existing contacts
}

返回以下结果:

{
    "total": 100,
    "data": [
        {
            "id": 1,
            "name": "Turner PLC",
            "addresses": [
                {
                    "id"         : 214,
                    "country_id" : 1,
                    "city"       : "North Jason",
                    "country"    : {
                        "id"        : 1,
                        "name"      : "Canada"
                    }
                },
                {
                    "id"         : 203,
                    "country_id" : 2,
                    "city"       : "West Lafayette",
                    "country"    : {
                        "id"        : 2,
                        "name"      : "The Netherlands"
                    }
                }
            ]
        }
    ]
}

API支持2种使用POST/PUT的方式:

  1. POST/PUT数据,结构与上面在数据"属性下显示的结构相同.后端将基于模型的id来决定是否需要创建或更新模型.后端还根据提供的有效负载自动检测是否需要创建/更新/删除关系.太酷了吧? :-)

  1. POST/PUT data in the same structure as displayed above under the "data" attribute. Based on the id of a Model, the backend will decide if it needs to create OR update a Model. The backend also automatically detects if it needs to create/update/delete relationships, based on the supplied payload. Pretty cool, huh? :-)

POST/api/v1/实体...

POST /api/v1/entities...

...具有以下有效负载:

... with the following Payload:

[
    {
        "type" : "Contact",
        "data" : {
            "id"   : null, // Will create a new Contact
            "name" : "Malaysia"
        }
    },
    {
        "type" : "Contact",
        "data" : {
            "id"   : 1, // Will update existing Contact
            "name" : "Turner Example"
        }
    },
    {
        "type" : "Address",
        "data" : {
            "id"          : 203, // Will update existing Address
            "city"        : "South Jason",
            "contacts_id" : [1,3,5] // Will set/update the many-to-many relationship between Address ID 203 and Contact ID 1, 3 and 5
        }
    }
]

现在我有以下问题:

  1. 这可以在BreezeJS中完成吗,还是应该考虑使用JayData或Waterline之类的替代方法?

  1. Can this be done in BreezeJS or should I consider an alternative like JayData or Waterline?

BreezeJS似乎坚持使用类似语法的查询.我错过了它的目的还是没有用?如果是这样,是否可以省略类似语法的查询,而仍然使用BreezeJS或替代方法?

BreezeJS seems te insist using a query like syntax. Am I missing its purpose or is it useless? If so, is it possible to omit the query like syntax and still use BreezeJS or an alternative?

BreezeJS(或替代方法)将如何处理hasOne,hasMany,belongsTo和belongsToMany和多态关系?

How will BreezeJS (or the alternative) deal with the hasOne, hasMany, belongsTo and belongsToMany and Polymorphic relationships?

与我的类似用例是否存在入门"?我已经阅读了很多有关BreezeJS及其替代品的内容,但是困惑的各个部分还没有浮现在我的脑海.换句话说,我完全迷路了.

Does a "getting started" exist for a similar use case as mine? I've read a lot about BreezeJS and its alternatives, but the pieces of the puzzle are not coming together in my mind yet. In other words, I'm completely lost.

推荐答案

http://上有很多示例www.breezejs.com/samples 展示了在没有EF和OData的情况下使用Breeze.js.选择您的口味,我会推荐Zza,SPA简介(Ruby),Edmunds或ESPN示例.他们都使用了我记得的客户端元数据.

There are plenty of samples at http://www.breezejs.com/samples that show using Breeze.js without EF and OData. Choose your flavor, I would recommend Zza, Intro to SPA (Ruby), Edmunds, or ESPN sample. They all use client-side metadata from what I recall.

就POST数据而言,可以使用 http://www之类的东西自定义AJAX适配器. breezejs.com/documentation/breezeajaxpostjs 允许发布数据,并且Breeze仍将结果解释为实体(如果它们处于适当的JSON结构中),或者您始终可以使用自己的扩充来扩展当前的AJAX适配器Breeze提供或仅使用普通的旧AJAX调用.真的没关系.

As far as POSTing data feel free to either customize the AJAX adapter using something like http://www.breezejs.com/documentation/breezeajaxpostjs which allows POSTing data and Breeze will still interpret the results as entities given they are in a proper JSON structure or you can always either extend the current AJAX adapter Breeze supplies with your own augmentations or just use plain old AJAX calls. It really doesn't matter.

请记住,Breeze不喜欢方法,只有这样做,您才可以获取/设置数据,并使用结果更新已缓存的实体.要专门回答您的问题-

Keep in mind that Breeze doesn't prefer how you get/set your data only that you do so and update the entities that it has cached with the results. To specifically answer your questions -

  1. 这可以在BreezeJS中完成吗,还是应该考虑使用JayData或Waterline之类的替代方法? 我应该使用这个还是那个? -一个不容易回答的问题.始终选择适合工作的最佳工具.

  1. Can this be done in BreezeJS or should I consider an alternative like JayData or Waterline? Should I use this or that? - Not an easy question to answer. Always choose the best tool for the job.

BreezeJS似乎坚持使用类似语法的查询.我错过了它的目的还是没有用?如果是这样,是否可以省略类似语法的查询,而仍然使用BreezeJS或替代方法? 默认情况下,Breeze在查询中使用该语法,因为默认适配器是WebAPI.随时使用.withParameters()方法创建我们自己的查询主体.

BreezeJS seems te insist using a query like syntax. Am I missing its purpose or is it useless? If so, is it possible to omit the query like syntax and still use BreezeJS or an alternative? By default Breeze uses the syntax in queries because the default adapter is the WebAPI one. Feel free to use the .withParameters() method to create our own query bodies.

BreezeJS(或替代方案)将如何处理hasOne,hasMany,belongsTo和belongsToMany和多态关系? 微风支持除多对多之外的所有关系和类型.但是,结点类型对象可以支持多对多关系. http://www.breezejs.com/documentation/presenting-many-many

How will BreezeJS (or the alternative) deal with the hasOne, hasMany, belongsTo and belongsToMany and Polymorphic relationships? Breeze supports all relationships and types except for many-to-many. Many-to-many relationships can be supported with a junction type object though. http://www.breezejs.com/documentation/presenting-many-many

与我的类似用例是否存在入门"?我已经阅读了很多有关BreezeJS及其替代品的内容,但是困惑的各个部分还没有浮现在我的脑海.换句话说,我完全迷路了. 阅读文档并查看上面的示例.在某些示例中有一些链接,我是ESPN的作者,所以我知道它是可行的,并且与API实现无关

Does a "getting started" exist for a similar use case as mine? I've read a lot about BreezeJS and its alternatives, but the pieces of the puzzle are not coming together in my mind yet. In other words, I'm completely lost. Read the docs and check out the samples above. There are links in some of the samples to a walk-thru, I am the author of the ESPN one so I know that it works and is unconcerned with the API implementation

这篇关于使用PHP REST API在Durandal上使用BreezeJS(Laravel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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