如何检查请求是否在Total.js控制器中发布数据

我想要一个解决scheme,通过validation用户是否提供了一些数据来保持相同function的查看和发布操作。

我的total.js控制器是这样的

# framework exports.install = (framework) -> framework.route "/subscribe", subscribe framework.route "/subscribe", subscribe, ['post'] # subscribe form and action subscribe = -> self = this post = self.post if post Subscriber = self.model('subscriber') Subscriber.save post self.view "form" 

问题是,当我只查看页面(不提交数据)时, post{} ,所以它总是input。

如果我比较{}if post isnt {} self.post if post isnt {} )条件总是为false,可能是因为self.post不是一个空对象。

[更新]

  • 查看页面时:

     console.log post #logs {} 
  • 提交表单时:

     console.log post #logs { type: 1, email: "email@example.com" } 

你不清楚is isnt 。 看看这个例子:

 coffee> {} is {} false 

令人惊讶? 不,如果你知道istesting是两个variables引用同一个对象。 不一样 。 真的是同一个对象:

 coffee> obj1 = {} {} coffee> obj2 = obj1 {} coffee> obj1 is obj2 true 

你可能会去==为了testing等价 。 但:

 coffee> {} == {} false 

不幸的是, JavaScript没有提供简单的方法来testing对象的相等性

在你的情况下,你应该采取一些伎俩。 喜欢:

 if (i for i in post).length == 0 ... 

或者,也许检查一些关键字段的存在:

 if 'type' not of post ... 

简单检查:

 function some_action_in_controller() { var self = this; // self.body is same as self.post var isEmpty = Object.keys(self.body).length === 0); self.json({ result: isEmpty }); }