Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • Angular Template Angular Template
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Issues 1
    • Issues 1
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • IzeniIzeni
  • Angular TemplateAngular Template
  • Wiki
  • data store

data store · Changes

Page history
add loadRelations example authored Aug 24, 2016 by Mikkel Davis's avatar Mikkel Davis
Show whitespace changes
Inline Side-by-side
data-store.md
View page @ 4ed2396a
jsdata
crud
```
class Book (models.Model):
title = models.CharField(max_length=50)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher, null=True, on_delete=models.SET_NULL)
// JSData v3, JSDataHttpAdapter v3
DataStore.defineMapper(name, { // 'Book'
// ... endpoint, etc
relations: {
belongsTo: {
Publisher: {
foreignKey: 'publisher',
localField: 'publisherObj',
},
},
hasMany: {
Author: {
localKeys: 'authors',
localField: 'authorObjs',
}
},
},
});
DataStore.defineMapper(name, { // 'Author'
// ... endpoint, etc
relations: {
hasMany: {
Book: {
foreignKeys: 'authors',
localField: 'books',
}
}
},
});
DataStore.defineMapper(name, { // 'Publisher'
// ... endpoint, etc
// This relationship to Book is only necessary if you want Publishers to have the 'books' array (named using localField), listing all books they are related to. But 'publisherObj' can still be loaded onto a Book even without this side of the relationship defined.
relations: {
hasMany: {
Book: {
foreignKey: 'publisher',
localField: 'books',
}
}
},
});
this.Book.readList().subscribe(books => {
// loadedBooks === [
// {
// id: 1,
// title: 'To Kill a Mockingbird',
// authors: [1],
// authorObjs: undefined,
// publisher: 1,
// publisherObj: undefined
// }
// ]
let bookPromises = books.map((book: any) => book.loadRelations(['publisherObj', 'authorObjs']));
Promise.all(bookPromises).then(loadedBooks => {
// loadedBooks === [
// {
// id: 1,
// title: 'To Kill a Mockingbird',
// authors: [1],
// authorObjs: [
// {
// id: 1,
// first_name: 'Harper',
// last_name: 'Lee',
// books: [
// {
// id: 1,
// title 'To Kill a Mockingbird',
// authorObjs: [ { id: 1, ... } ],
// ...
// }
// ]
// }
// ],
// publisher: 1,
// publisherObj: {
// id: 1,
// name: 'HarperCollins',
// books: [
// {
// id: 1,
// title: 'To Kill a Mockingbird',
// publisherObj: { id: 1, ... },
// ...
// }
// ]
// }
// }
// ]
});
});
```
\ No newline at end of file
Clone repository
  • angular
  • cookbook forms
  • cookbook routing
  • data store
  • gotchas
  • Home
  • modals
  • sass with bem
  • toolbox
  • typescript
  • webpack