Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jquintozamora/43d61763771078d72a81890a63605ebc to your computer and use it in GitHub Desktop.
Save jquintozamora/43d61763771078d72a81890a63605ebc to your computer and use it in GitHub Desktop.
import { Item, ODataEntity } from "sp-pnp-js";
export class MyItem extends Item {
public static Fields = ["Id", "Title", "Category", "Quantity"];
public Id: number;
public Title: string;
public Category: string;
public Quantity: number;
// override get to enfore select for our fields to always optimize
// but allow it to be overridden by any supplied values
public get(): Promise<MyItem> {
// use apply and call to manipulate the request into the form we want
// if another select isn't in place, let's default to only ever getting our fields.
const query = this._query.getKeys().indexOf("$select") > -1 ? this : this.select.apply(this, MyItem.Fields);
// call the base get, but in our case pass the appropriate ODataEntity def so we are returning
// a MyItem instance
return super.get.call(query, ODataEntity(MyItem), arguments[1] || {});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment