Coder Social home page Coder Social logo

ODATA about ngx-resource HOT 10 CLOSED

troyanskiy avatar troyanskiy commented on June 16, 2024
ODATA

from ngx-resource.

Comments (10)

troyanskiy avatar troyanskiy commented on June 16, 2024

Hello @beradrian .
I have never worked with ODATA. If you are 100% sure that your resource class name will be used in path, than yes, you can overwrite _getPath or getPath methods. Otherwise better to define path for each request separately.

from ngx-resource.

beradrian avatar beradrian commented on June 16, 2024

By default, the path will be the class name (or start with it). What's the difference between _getPath and getPath? Which one should I override in this case?

from ngx-resource.

beradrian avatar beradrian commented on June 16, 2024

And another question - what will be the best way to define the base URL across all resources?

from ngx-resource.

beradrian avatar beradrian commented on June 16, 2024

There's also the problem that _getPath is marked as private.

from ngx-resource.

troyanskiy avatar troyanskiy commented on June 16, 2024

@beradrian so the best for you will be override getPath method, not _getPath.
To define global base URL is better to use static class somewhere in your app component:

ResourceGlobalConfig.url = 'https://api.mydomain.com'

from ngx-resource.

beradrian avatar beradrian commented on June 16, 2024

And if I override getPath can I still use the path property of the annotation?

from ngx-resource.

beradrian avatar beradrian commented on June 16, 2024

I have the following

export class ODATAResource<R> extends Resource {

    @ResourceAction({
        path: '/{!id}'
    })
    get: ResourceMethod<{id: any}, R>;

    getPath(): string | Promise<string> {
        return "/" + this.getPlural();
    }

    private getName() {
        return (<any>this).constructor.name;
    }

    private getPlural() {
        return this.getName() + "s";
    }
}


@Injectable()
@ResourceParams()
export class UserResource extends ODATAResource<User> {
}

The problem is that when I call userResource.get({id: "1"}) the URL http://localhost/1 gets called instead of http://localhost/Users/1 as I would have expected. Am I missing something?

from ngx-resource.

troyanskiy avatar troyanskiy commented on June 16, 2024

In your case getPath is also overridden by method decorator

    @ResourceAction({
        path: '/{!id}'
    })
    get: ResourceMethod<{id: any}, R>;

from ngx-resource.

troyanskiy avatar troyanskiy commented on June 16, 2024

So to resolve that I think better to override getUrl, which will return full path with your class name. In that case you can decorate methods with path param.

export class ODATAResource<R> extends Resource {

    @ResourceAction({
        path: '/{!id}'
    })
    get: ResourceMethod<{id: any}, R>;

    getUrl(): string | Promise<string> {
        return super.getUrl() + "/" + this.getPlural();
    }

    private getName() {
        return (<any>this).constructor.name;
    }

    private getPlural() {
        return this.getName() + "s";
    }
}


@Injectable()
@ResourceParams()
export class UserResource extends ODATAResource<User> {
}

Final url is created by kind of getUrl() + getPath()

from ngx-resource.

beradrian avatar beradrian commented on June 16, 2024

I would have expected that the url/path of the method to be relative to the one of the class, which will also be relative to the global one.

Anyway, wanted to thank you for a great library.

from ngx-resource.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.