2016-06-16 5 views
1

私は$ saveを使ってリソースをどれだけ正確に更新するのか混乱しています。私は角度のリソースのドキュメントを読んで、スタックオーバーフローに関する他の投稿を見ましたが、既存のオブジェクトに対して更新操作を実行できないようです。

たとえば、イベントオブジェクトがあり、名前と場所のプロパティを更新したいとします。私は、特異イベントのイベントIDを正しく取り込む関数の開始点を持っています。ここで

は、これまでの機能である:

eventService.updateEvent = function (eventId, eventName, eventLocation) { 

    // Defines the resource (WORKS) 
    var Event = $resource('/api/events/:id/', {id:'@_id'}); 

    // Gets the event we're talking about (WORKS) 
    var event = Event.get({'id': eventId}); 

    // TODO update event 

    }; 

にはどうすれば正常にこのリソースを更新していますか?

答えて

0

それを実感してください!

リソースを定義したとき、PUT操作を「更新」というカスタムメソッドとして定義しました。

私はそのリソースを取得し、特定のオブジェクトをIDで検索しました。 約束を使用して、オブジェクトが見つかった場合は「更新メソッド」を使用してリソースを更新できました。それ以外の場合はエラーをスローしました。

eventService.updateEvent = function (eventId,eventName,eventLocation) { 

    // Define the event resource, adding an update method 
    var Event = $resource('/api/events/:id/', {id:'@_id'}, 
    { 
     update: 
     { 
      method: 'PUT' 
     } 
    }); 

    // Use get method to get the specific object by ID 
    // If object found, update. Else throw error 
    Event.get({'id': eventId}).$promise.then(function(res) { 
     // Success (object was found) 

     // Set event equal to the response 
     var e = res; 

     // Pass in the information that needs to be updated 
     e.name = eventName; 
     e.location = eventLocation; 

     // Update the resource using the custom method we created 
     Event.update(e) 

    }, function(errResponse) { 
     // Failure, throw error (object not found) 
     alert('event not found'); 
    }); 

}; 
関連する問題