简体   繁体   English

如何在Tower.js中使用一对多关系?

[英]How do I use One-to-Many Relationships in Tower.js?

So, I've been generating different scaffolds trying to use the One-to-Many relationship features of Tower, but I can't figure out how to actually link up the related instances to make use of the relationship. 因此,我一直在尝试使用Tower的一对多关系功能生成不同的支架,但是我不知道如何实际链接相关实例以利用该关系。 For instance, my most recent scaffolds were generated like this: 例如,我最近的支架是这样生成的:

tower generate scaffold User email:string firstName:string lastName:string hasMany:posts

tower generate scaffold Post title:string body:text userId:integer belongsTo:user

Now, in rails, that userId field, on the post model, would form a connection with the user sharing that id, and then you could use that to access the relationship. 现在,在Rails中,帖子模型上的userId字段将与共享该ID的用户建立连接,然后您可以使用它来访问关系。 But here it doesn't seem to do anything. 但是在这里似乎什么也没做。 Trying to use any of the code from here: https://github.com/viatropos/tower/wiki/1-n just gives me errors. 尝试从此处使用任何代码: https : //github.com/viatropos/tower/wiki/1-n只会给我错误。

In the tower console, I was able to create an example user and post (I can also do this easily enough by running the server and using the forms on the web page), like so: 在塔式控制台中,我能够创建一个示例用户并发布(通过运行服务器并使用网页上的表单,我也可以很容易地做到这一点),如下所示:

tower> user = new App.User

tower> user.attributes = { email: "bill@bill.com", firstName: "bill", lastName: "billiams" }

tower> post = new App.Post

tower> post.attributes = { title: "A Post", body: "This is a short post.", userId: "4fbf23224503fe670e000006" }

And these instances are persisted to the database, but when I try code like: 这些实例将持久化到数据库中,但是当我尝试如下代码时:

tower> user.get('posts').exists()

I get "TypeError: Cannot call method 'exists' of undefined". 我收到“ TypeError:无法调用未定义的方法”。 Similarly, calling: 同样,调用:

tower> user.get('posts').create(title: 'Berlin never sleeps.')

produces "TypeError: Cannot call method 'create' of undefined". 产生“ TypeError:无法调用未定义的方法'create'”。 The same sort of thing happens if I try anything like this in the models or controllers. 如果我在模型或控制器中尝试类似的操作,也会发生同样的事情。 I'm really stuck here, and have been trying for several days to figure out how this works, but I don't know where else to look. 我真的被困在这里,并且已经尝试了好几天来弄清楚它是如何工作的,但是我不知道还有什么地方。 If anyone has example code I could peek at, that would be awesome, otherwise an explanation of sorts would be helpful also. 如果任何人都有我可以看到的示例代码,那将是非常棒的,否则进行各种解释也将很有帮助。 Thanks. 谢谢。

[Edit: My working example, along with detailed README explaining the steps, can be found here: https://github.com/edubkendo/demoApp ] [编辑:我的工作示例以及详细的自述文件解释了这些步骤,可以在这里找到: https : //github.com/edubkendo/demoApp ]

Right now relations are accessed through methods rather than getters: 现在,关系是通过方法而不是获取方法访问的:

user.posts().all (error, posts) -> console.log(posts)

the posts() method returns a Tower.Model.Relation.HasMany.Scope object, which is a subclass of Tower.Model.Scope . posts()方法将返回Tower.Model.Relation.HasMany.Scope对象,该对象是Tower.Model.Scope的子类。 The "scope" is what allows you to do queries on the database: “作用域”使您可以对数据库进行查询:

user.posts().where(title: /a/).asc('createdAt').all()

Just for reference, here's how the relations are built. 仅供参考,这里是建立关系的方式。 First, when you do something like: 首先,当您执行以下操作时:

class App.User extends Tower.Model
  @hasMany 'posts'

that @hasMany 'posts' goes to /tower/model/relations.coffee#L50 , which constructs a Tower.Model.Relation.HasMany object, which extends Tower.Model.Relation . @hasMany 'posts'转到/tower/model/relations.coffee#L50 ,该对象构造一个Tower.Model.Relation.HasMany对象,该对象扩展了Tower.Model.Relation The Tower.Model.Relation class defines the association methods/getters on the owner class, the App.User : /tower/model/relation.coffee#L85 . Tower.Model.Relation类在ownerApp.User上定义关联方法/ App.User/tower/model/relation.coffee#L85

When you do user.posts() , it constructs a new Tower.Model.Relation.HasMany.Scope : /tower/model/relations.coffee#L85 , which gives you all the finder/persistence methods, the same as doing App.User.asc('createdAt').all() . 当执行user.posts() ,它将构造一个新的Tower.Model.Relation.HasMany.Scope/tower/model/relations.coffee#L85 ,它为您提供所有查找程序/持久性方法,与执行App.User.asc('createdAt').all()相同App.User.asc('createdAt').all() The relation subclasses Tower.Model.Scope in order to customize find/create/update/delete methods for the association, that's pretty much it. 为了自定义关联的查找/创建/更新/删除方法,该关系将子类Tower.Model.Scope子类化,仅此而已。

Last thing, the api is going to be changed within the next few weeks as we normalize it with the field methods (ie user.get('email') ). 最后,随着我们使用字段方法(即user.get('email') )对其进行规范化,该API将在接下来的几周内进行更改。 So soon, it will look like this: 很快,它将如下所示:

user.get('posts').all()
user.posts.all() # a getter, if getter/setter support is available in the browser, and it is available in node.js

But for now, use this: 但现在,使用此命令:

user.posts().all (error, posts) ->

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM