AngularJS如何仅在$resource实例上存储客户端信息

时间:2022-07-21 19:41:14

I'm trying to figure out "the right way" to store client-side-only state for an instance of a resource.

我试图找到“正确的方式”来存储资源的一个实例的客户端状态。

Suppose I have an ng-resource, Blerg which has only one data field, "name" and per the docs, I do something like

假设我有一个ng-resource, Blerg只有一个数据字段,“name”,对于文档,我做了一些类似的事情

var blergInstance = new Blerg();
blergInstance.name = "Frankie";
blergInstance.$save();

This results in an http POST to the resource URL with json {name: "Frankie"}. Ok, great.

这会导致一个http POST到具有json {name:“Frankie”}的资源URL。好了,好了。

But I actually need to pass that blergInstance around my client-side application interface for a while, and I want to store some state about that blergInstance to tell the client-side application how to display it or how to interact with it. E.g. I've got a directive that wants to optionally display to the user that "Your blergInstance hasn't been saved yet". Or I've got a button elsewhere that will "Moog this Blerg", and I only want to allow a Blerg to be Mooged once before saving.

但实际上,我需要在客户端应用程序接口周围传递blergInstance一段时间,我想要存储关于该blergInstance的一些状态,以告诉客户端应用程序如何显示它或如何与它交互。我有一个指令,可以随意地向用户显示“您的blergInstance还没有保存”。或者我在别的地方有一个按钮,它会“咕噜咕噜”,我只希望在保存之前允许咕噜咕噜咕噜咕噜地响一次。

My first (admittedly naive) approach would be to do something like

我的第一个方法(当然是天真的)是做一些类似的事情

var blergInstance = new Blerg();
blergInstance.name = "Frankie";
blergInstance.saved = false //
blergInstance.hasBeenMooged = false // 
// pass the blergInstance around to other services, directives, etc
blergInstance.$save();

But now the http POST looks like {name: "Frankie", saved: false, hasBeenMooged: false}.

但是现在http POST看起来像{name:“Frankie”,保存了:false, haswas mooged: false}。

So how should I attach "state" to the resource instance that is only relevant to the client-side, and which should not be sent to the server?

那么,我应该如何将“state”附加到只与客户端相关、不应该发送到服务器的资源实例?

2 个解决方案

#1


2  

Why shouldn't you wrap the resource and state into an simple object and pass around, where resource will have the necessary properties only

为什么不将资源和状态打包到一个简单的对象中,并在资源只有必要属性的地方进行传递呢

var stateful = {};
var blergInstance = new Blerg();
blergInstance.name = "Frankie";

stateful.resource = blergInstance;
stateful.saved = false;
stateful.hasBeenMooged = false;

// pass the blergInstance around to other services, directives, etc
stateful.resource.$save();

#2


0  

Here's an alternative to code-jaff's solution

这是另一种代码-jaff解决方案

Warning: coffeescript ahead

警告:coffeescript之前

Step 1: Create a service with all your API calls

步骤1:创建一个包含所有API调用的服务

app.service 'Api', ['$resource', ($resource) ->
    {
        Blerg: $resource "/api/v1/blerg/:id", {id: "@id"}
        ....
    }
]

Step 2: Call your Api service and pass in an explicit object

步骤2:调用Api服务并传入显式对象

app.controller 'Ctrl', (Api) ->
    saveBlerg = {}
    saveBlerg.name = Blerg.name
    Api.Blerg.save(saveBlerg)

#1


2  

Why shouldn't you wrap the resource and state into an simple object and pass around, where resource will have the necessary properties only

为什么不将资源和状态打包到一个简单的对象中,并在资源只有必要属性的地方进行传递呢

var stateful = {};
var blergInstance = new Blerg();
blergInstance.name = "Frankie";

stateful.resource = blergInstance;
stateful.saved = false;
stateful.hasBeenMooged = false;

// pass the blergInstance around to other services, directives, etc
stateful.resource.$save();

#2


0  

Here's an alternative to code-jaff's solution

这是另一种代码-jaff解决方案

Warning: coffeescript ahead

警告:coffeescript之前

Step 1: Create a service with all your API calls

步骤1:创建一个包含所有API调用的服务

app.service 'Api', ['$resource', ($resource) ->
    {
        Blerg: $resource "/api/v1/blerg/:id", {id: "@id"}
        ....
    }
]

Step 2: Call your Api service and pass in an explicit object

步骤2:调用Api服务并传入显式对象

app.controller 'Ctrl', (Api) ->
    saveBlerg = {}
    saveBlerg.name = Blerg.name
    Api.Blerg.save(saveBlerg)