2012-06-28 9 views
15

これは奇妙なことかもしれませんが、モジュールのファクトリメソッドを使用して$ resourceのデフォルトPOSTデータを指定する必要があります。

誰でもAngularJSでこれを行う方法がありますか?

EDIT:データは私の将来POSTリクエストのデフォルトのデータである

/** 
* Module declaration. 
* @type {Object} 
*/ 
var services = angular.module("services", ["ngResource"]); 

/** 
* Product handler service 
*/ 
services.factory("Product", function($resource) { 
    return $resource("http://someUrl", {}, { 
     get : {method: "GET", params: {productId: "-1"}}, 
     update: {method : "POST", params:{}, data: {someDataKey: someDataValue}} 
    }); 
});

さて、私はこのような何かをしたいです。

+1

あなたはそのデータをどのように送信しますか?もしあなたがトークンを送るなら、あなたはpostDataの中で必ずしもそうではなく、ヘッダーでそれを送るべきです... – inf3rno

答えて

18

これは、データ整合性が失われ、モデルに反映されていないようなものではありません。

なぜですか?

リソースファクトリはオブジェクトを作成し、オブジェクトインスタンスデータをPOSTとして使用します。私はdocumentationとangular-resource.jsを見てきましたが、angle-resource.jsを変更せずに、リソースによって作成されるオブジェクトのデフォルトのカスタムプロパティを指定する方法はないようです。あなたは何ができるか

は次のとおりです。

services.factory("Product", function($resource) { 
    return $resource("http://someUrl", {}, { 
     get : {method: "GET", params: {productId: "-1"}}, 
     update: {method : "POST"} 
    }); 
}); 

とあなたのコントローラで:

$scope.product = {}; // your product data initialization stuff 
$scope.product.someDataKey = 'someDataValue'; // add your default data 

var product = new Product($scope.product); 
product.$update(); 
+1

私はそれに固執する必要がありますデフォルトのPOST値を設定することができます:) – Nimaen

0

ラッパー機能が動作します。

function myPost(data) { 
    return $http.post('http://google.com', angular.extend({default: 'value'}, data)) 
} 

myPost().success(function(response) { ... }); 
+0

それは確かに動作しますが、あなたの答えの直後に私が指摘した方法ではありません:) – Nimaen

2

私はそれはあなたが更新機能を呼び出す方法によって異なりますと思います。角メインページのチュートリアル「バックアップを配線する」を読むと、mongolab.jsは「プロジェクト」ファクトリを提供します。そのままコピー:あなたは、あなたの場合は

project.update(someFunction); 

project = Project.get({id:1}); 

はその後いくつかの変更後にアップデートを行います。

angular.module('mongolab', ['ngResource']). 
factory('Project', function($resource) { 
    var Project = $resource('https://api.mongolab.com/api/1/databases' + 
     '/angularjs/collections/projects/:id', 
     { apiKey: '4f847ad3e4b08a2eed5f3b54' }, { 
     update: { method: 'PUT' } 
     } 
); 

    Project.prototype.update = function(cb) { 
    return Project.update({id: this._id.$oid}, 
     angular.extend({}, this, {_id:undefined}), cb); 
    }; 

    Project.prototype.destroy = function(cb) { 
    return Project.remove({id: this._id.$oid}, cb); 
    }; 

    return Project; 
}); 

使い方はあなたが最初のプロジェクトのインスタンスを取得するということです常に必要なデータを追加するように更新プログラムを変更することができます。

Product.prototype.update = function(cb) { 
    return Product.update({}, 
     angular.extend({}, this, {someDataKey: someDataValue}), cb); 
}; 

そうしないと、あなたが最も可能性の高いのparamsにキー/値のペアを置くことができます。

update: {method : "POST", params:{someDataKey: someDataValue}} 

それはURLでキー/値ペアで掲載されますが、ほとんどのアプリケーションサーバは、最近にペアをスローしますとにかくparamsオブジェクト。

0

これで問題を解決できますか?

services.factory("Product", function($resource) { 
    return $resource("http://someUrl", {}, { 
    get : {method: "GET", params: {productId: "-1"}}, 
    update: {method : "POST", params:{}, data: {someDataKey: someDataValue}} 
    }); 
}); 
services.factory("DefaultProduct", function(Product) { 
    return function(){ 
    return new Product({ 
     data:"default"; 
    }); 
    }; 
}); 
services.controller("ProductCTRL",function($scope,DefaultProduct){ 
    $scope.product = new DefaultProduct(); 
}); 
0

デフォルトでは、mergeのパラメータを使用できます。すべてparamsで利用可能ですが、デフォルトオブジェクトで提供されます。利用できるすべてはmyParamsは、JSONオブジェクトなどの変数とmyDefaultデフォルト値のリストになりmyParams

services.factory("Product", function($resource) { 
    return $resource("http://someUrl", {}, { 
     get : {method: "GET", params: {productId: "-1"}}, 
     update: {method : "POST", params:angular.extend(myDefault, myParams);} 
    }); 
}); 

によって上書きされます。

0

あなたの$リソースPOSTメソッドを使用の行動のためにtransformRequestオプションを使用して、要求に応じて、デフォルトのフィールドを設定することができます。この

function prependTransform(defaults, transform) { 

// We can't guarantee that the default transformation is an array 
defaults = angular.isArray(defaults) ? defaults : [defaults]; 

// Append the new transformation to the defaults 
return [transform].concat(defaults); 
} 

ctrl.factory('MyResource', ['$resource', '$http', 
function($resource, $http) { 
    return $resource('/path/to/myresource/:id', {id : '@id'}, 
      { 
       create : { 
        method : 'POST', 
        transformRequest : prependTransform($http.defaults.transformRequest, 
         function(data, headers) { 
          return addDefaultField(data); 
         } 
       ), 
       }, 
      }); 
    } 
]); 
2

よう

例えば何かが、私はほとんどがドキュメントhereで小さな宝石を見逃していると思います。

non-GET "class" actions: Resource.action([parameters], postData, [success], [error]) 

これは、次のことができることを示しています。

var User = $resource('/user'); 
postData = { name : 'Sunil', 'surname' : 'Shantha' }; 

var user = User.save({notify:'true'}, postData, function() { 
    // success! 
}); 

セーブアクション(ポスト)を行うときの2番目のパラメータは、ポストデータです。