Optimising the Details Page

In notebook:
Building Modern Web Apps
Created at:
2016-01-28
Updated:
2016-01-28
Tags:
libraries JavaScript React Ampersand
So far (see previous note about create a new repo model) we're only creating a new model if we don't find it in the collection. Now we need to actually fetch one.

Also we need to fetch the details of the repo
  // ****     repo-collection.js      ****
import Collection from 'ampersand-rest-collection'
import Repo from './repo'
import githubMixin from '../helpers/github-mixin'

export default Collection.extend({
    url: 'https://api.github.com/user/repos', 
    model: Repo, 

    getByFullName (fullName) {
        let model = this.findWhere({full_name: fullName})
        
        if(!model){
            model = new Repo({full_name: fullName})
        }
        
        // ++++ 1. now we can fetch the repo (we have a model)
        // since we're on a detail page it's OK to refetch
        // even if already have a model
        // we could move it above into the if statement
        model.fetch()
        
        return model
    }
})
Checks the github documentation (developer.github.com/v3/repos) to see how to get the details.
​GET /repos/owner/repo

Now, in models/repo.js we cannot use the usual method of adding an ​url​ property with a static string since we need to calculate it dynamically for each repo.
  // ****     repo.js    ****
import Model from 'ampersand-model'

export default Model.extend({
    // ++++ 1. add the url so that we can fetch
    // url: // we need something dynamic here
    // use a function
    url () {
        return 'https://api.github.com/repos/' + this.full_name
    }
    
    props: {
        id: 'number',
        name: 'string',
        full_name: 'string'
    }

    derived: {
        appUrl: {
            deps: ['full_name'],
            fn () {
                return '/repo/' + this.full_name
            }
        }
    }
    
})
So we're fetching multiple times. The page will still load immediately because we already hove the collection in localstorage. Then it will refetch and update the view with new data if necessary.He didn't pass the token, so repo detail page should not be availablePrivate repos: should not give a 401, because you give away information that the user has a repo by that name.

So if you have 404 from Github (as someone from the group asked), means you didn't pass the authorisation.

We need to add that mixin. The model has to have that information.
  // ****     repo.js    ****
import Model from 'ampersand-model'
// ++++ 1. import the github mixin
import githubMixin from '../helpers/github-mixin'

// ++++ 2. pass the github mixin as a paremeter 
// to Model.extend
// ---- instead of this
// export default Model.extend({
// ++++ add githubMixin
export default Model.extend(githubMixin, {
    url () {
        return 'https://api.github.com/repos/' + this.full_name
    }
    
    props: {
        id: 'number',
        name: 'string',
        full_name: 'string'
    }

    derived: {
        appUrl: {
            deps: ['full_name'],
            fn () {
                return '/repo/' + this.full_name
            }
        }
    }
    
})