English 中文(简体)
我该如何使用塔台的一对多的关系?
原标题:How do I use One-to-Many Relationships in Tower.js?

所以,我一直在制造不同的脚手架, 试图使用塔的一对多关系特征, 但我想不出如何将相关事件连接起来 来利用关系。例如,我最新的脚手架是这样产生的:

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

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

现在,在铁路上,在邮政模型上的用户Id字段将形成用户共享该代号的连接, 然后你就可以使用该代号访问关系。 但在这里,它似乎没有做任何事情。 试图使用这里的任何代码: < a href="https://github.com/viatropos/tower/wiki/1-n" rel="nofollow" >https://github.com/viatropos/tower/wiki/1-n 只会给我错误。

在塔台控制台里, 我得以创建了一个示例用户和邮递站(我也能通过管理服务器和使用网页上的表格, 很容易做到这点), 例如:

tower> user = new App.User

tower> user.attributes = { email: "[email protected]", firstName: "bill", lastName: "billiams" }

tower> post = new App.Post

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

这些例子一直持续到数据库, 但是当我尝试代码时,比如:

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

我得到了“ TypeError: 无法调用未定义的方法 ” 。 同样, 调用 :

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

产生“ TypeError : 无法调用方法创建未定义 ” 。 如果我在模型或控制器中尝试类似的方法, 也会发生同样的事情。 我被困在这里, 并且已经尝试了好几天来想弄清楚它是如何运作的, 但我不知道还能看到哪里。 如果有人有我可以偷看的代码, 那会很棒, 否则解释一下也会有用。 谢谢 。

[编辑:我的工作实例,连同详细解释步骤的README,可在此找到:https://github.com/edubkendo/demoApp

最佳回答

目前,通过方法而不是搭便车接触关系:

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

posts () 方法返回 Tower.model. relation.HasMany.Scope 对象,这是 Tower.Model.Scope 的子类。 “ 镜”是允许您在数据库查询的:

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

仅供参考,这里是关系是如何建立起来的。首先,当你做一些类似的事情时:

class App.User extends Tower.Model
  @hasMany  posts 

< a href= relations >/tower/model/ relations/relations.coffee#L50 ,它建构了一个 Tower.Model.Musmany 对象,它扩展了 Tower.Model.Relation < a/codepp.repl > App.user_flictor < a/code >: < a hrefrefre16=

当您做 user. posts () , 它构建了一个新的 Tower. Model. relation. HasMany.Scope : App. User. as( c( 创建的At.) all () 有关子类 < Code>, 以自定义查找/ indation/ fate/ fate/ fate/ date/ date/ date/ deteate/ deteed/ date/ date/ deeted.

最后一点是,在未来几周内,当我们将其与字段方法(即 user.get(email) ) 正常化时,pi 将会被更改。 很快, 它会看起来是这样的 :

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

但现在,使用这个:

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

希望这有帮助。

问题回答

暂无回答




相关问题
How do I define global variables in CoffeeScript?

On Coffeescript.org: bawbag = (x, y) -> z = (x * y) bawbag(5, 10) would compile to: var bawbag; bawbag = function(x, y) { var z; return (z = (x * y)); }; bawbag(5, 10); compiling via ...

I m trying to re-write this in CoffeeScript. Coming unstuck

function getElementsByClassName(className) { // get all elements in the document if (document.all) { var allElements = document.all; } else { var allElements = document.getElementsByTagName("...

Anonymous functions syntax in CoffeeScript

I ve been looking at CoffeeScript and I m not understanding how you would write code like this. How does it handle nested anonymous functions in its syntax? ;(function($) { var app = $....

Why are my CoffeeScript/backbone.js events not firing?

I m trying to familiarize myself with CoffeeScript and backbone.js, and I must be missing something. This CoffeeScript: MyView = Backbone.View.extend events: { "click" : "testHandler" ...

Custom compiler with maven

I m trying to make Maven2 to compile coffeescript to javascript. As far as I m concerned there is no plugin which provides compiling coffeescript. Is there a compiler-plugin for maven which can be ...

what is wrong when I compile my .coffee files

Hi there Iam using coffeeScript for my apps now and I love it but recently I ve been having a lot of trouble with compilation, Iam using it for a rails application and when I run coffee -w -c public/...

CoffeeScript on Windows?

How can I try CoffeeScript on Windows? The installation instructions are only for *nix: http://jashkenas.github.com/coffee-script/#installation EDIT: Since I asked this a while ago, many new ...

How can I compile CoffeeScript from .NET?

I want to write an HttpHandler that compiles CoffeeScript code on-the-fly and sends the resulting JavaScript code. I have tried MS [JScript][1] and IronJS without success. I don t want to use [Rhino][...