A Class Method for Collections - ldco2016/microurb_web_framework GitHub Wiki
import { User, UserProps } from "./models/User";
import { Collection } from "./models/Collection";
const collection = new Collection<User, UserProps>(
"http://localhost:3000/users",
(json: UserProps) => User.buildUser(json)
);
collection.on("change", () => {
console.log(collection);
});
collection.fetch();
I am going to try to move this new Collection
stuff into a better location than my index.ts
file. I think the best place for it would be the User.ts
file. I think I should create a second static
class method inside of here that will return a collection of users.
So inside of User.ts
I am going to import Collection
like so:
import { Model } from './Model';
import { Attributes } from './Attributes';
import { ApiSync } from './ApiSync';
import { Eventing } from './Eventing';
import { Collection } from './Collection';
export interface UserProps {
id?: number;
name?: string;
age?: number;
}
const rootUrl = "http://localhost:3000/users";
export class User extends Model<UserProps> {
static buildUser(attrs: UserProps): User {
return new User(
new Attributes<UserProps>(attrs),
new Eventing(),
new ApiSync<UserProps>(rootUrl)
);
}
}
Then I added a new static
class method called buildUserCollection
like so:
import { Model } from "./Model";
import { Attributes } from "./Attributes";
import { ApiSync } from "./ApiSync";
import { Eventing } from "./Eventing";
import { Collection } from "./Collection";
export interface UserProps {
id?: number;
name?: string;
age?: number;
}
const rootUrl = "http://localhost:3000/users";
export class User extends Model<UserProps> {
static buildUser(attrs: UserProps): User {
return new User(
new Attributes<UserProps>(attrs),
new Eventing(),
new ApiSync<UserProps>(rootUrl)
);
}
static buildUserCollection() {}
}
This will return an instance of Collection
like so:
``javascript import { Model } from "./Model"; import { Attributes } from "./Attributes"; import { ApiSync } from "./ApiSync"; import { Eventing } from "./Eventing"; import { Collection } from "./Collection";
export interface UserProps { id?: number; name?: string; age?: number; }
const rootUrl = "http://localhost:3000/users";
export class User extends Model { static buildUser(attrs: UserProps): User { return new User( new Attributes(attrs), new Eventing(), new ApiSync(rootUrl) ); }
static buildUserCollection(): Collection<> {} }
And I have to specify the generic types for this thing, so the first is the `User` class and the second is the `UserProps` like so:
```javascript
import { Model } from "./Model";
import { Attributes } from "./Attributes";
import { ApiSync } from "./ApiSync";
import { Eventing } from "./Eventing";
import { Collection } from "./Collection";
export interface UserProps {
id?: number;
name?: string;
age?: number;
}
const rootUrl = "http://localhost:3000/users";
export class User extends Model<UserProps> {
static buildUser(attrs: UserProps): User {
return new User(
new Attributes<UserProps>(attrs),
new Eventing(),
new ApiSync<UserProps>(rootUrl)
);
}
static buildUserCollection(): Collection<User, UserProps> {}
}
Then inside of there I will return everything I put inside of index.ts
like so:
import { Model } from "./Model";
import { Attributes } from "./Attributes";
import { ApiSync } from "./ApiSync";
import { Eventing } from "./Eventing";
import { Collection } from "./Collection";
export interface UserProps {
id?: number;
name?: string;
age?: number;
}
const rootUrl = "http://localhost:3000/users";
export class User extends Model<UserProps> {
static buildUser(attrs: UserProps): User {
return new User(
new Attributes<UserProps>(attrs),
new Eventing(),
new ApiSync<UserProps>(rootUrl)
);
}
static buildUserCollection(): Collection<User, UserProps> {
return new Collection<User, UserProps>(
"http://localhost:3000/users",
(json: UserProps) => User.buildUser(json)
);
}
}
Now in this case I already defined the rootUrl
and I can reuse it like so:
import { Model } from "./Model";
import { Attributes } from "./Attributes";
import { ApiSync } from "./ApiSync";
import { Eventing } from "./Eventing";
import { Collection } from "./Collection";
export interface UserProps {
id?: number;
name?: string;
age?: number;
}
const rootUrl = "http://localhost:3000/users";
export class User extends Model<UserProps> {
static buildUser(attrs: UserProps): User {
return new User(
new Attributes<UserProps>(attrs),
new Eventing(),
new ApiSync<UserProps>(rootUrl)
);
}
static buildUserCollection(): Collection<User, UserProps> {
return new Collection<User, UserProps>(rootUrl, (json: UserProps) =>
User.buildUser(json)
);
}
}
Now I can go back over to index.ts
and to create my collection, I can call User.buildUserCollection()
like so:
import { User, UserProps } from "./models/User";
import { Collection } from "./models/Collection";
const collection = User.buildUserCollection();
collection.on("change", () => {
console.log(collection);
});
collection.fetch();
Now I no longer need to import UserProps
nor Collections
.
import { User } from "./models/User";
const collection = User.buildUserCollection();
collection.on("change", () => {
console.log(collection);
});
collection.fetch();