javascript REST客户端是否有任何最佳实践。具体来说,我想知道如何关注链接。假设我有一个任务类
public class Task
{
public long TaskId { get; set; }
public string Subject { get; set; }
public bool Completed { get; set; }
public bool Canceled { get; set; }
public List<Link> Links { get; set; }
}
假设这个服务模型类的实例被发送到 javascript 客户端:
{
TaskId : '1',
Subject : 'Some',
Completed: false,
Canceled : false,
Links : [{Rel:'Completed', URL:'http://myadress/tasks/1/complete'},
{Rel:'Canceled ', URL:'http://myadress/tasks/1/cancel'}]
}
现在我想完成这个任务。这应该通过使用 Rel 的链接来完成:已完成。但是怎么做呢?我应该只发布到 URL 并获取任务的新表示,还是应该创建类似的东西
var x = {
TaskId : '1',
Subject : 'Some',
Completed: false,
Canceled : false,
complete : function(callback){
// post to completed URL, and callback with new representation
}
Links : [{Rel:'Complete', URL:'http://myadress/tasks/1/complete'},
{Rel:'Cancel ', URL:'http://myadress/tasks/1/cancel'}]
}
x.complete(callback);
还是创建专用功能?
function completeTask(task, callback){
var url = //search links property for rel:Colmplete and get URL
//complete task
//callback with the new representation
}