Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

findOne based on route

$
0
0
Hi,

I've just with CanJS so this is probably a simple questiong. I haven an API that has these paths and responses:
  1. /api/:owner/:repo
  2. [{commit: '4asd56f4asd56f4asd65f', message: 'Commit message 1'}, {commit: 'df4saf54d6safd54asf6', message: 'Commit message 2'}]
and
  1. /api/:owner/:repo/:commit
  2. {commit_id: '4asd56f4asd56f4asd65f', message: 'Commit message 1', author: 'Frank'}

For this I have a Model:

  1. findAll: 'GET /api/github/{owner}/{repo}'
  2. findOne: 'GET /api/github/{owner}/{repo}/{commit}'


Now I want to show this data depending on the URL (e.g. /frk/repo1/ shows a list of commits, /frk/repo1/4asd56f4asd56f4asd65f shows details about one commit).

I have an app state object where I define a get function for 'commits' based on the owner and repo:

  1.     commits: {
          get: () ->
            new Commit.List {
              owner: this.attr('owner')
              repo: this.attr('repo')
            }

The template for the repo prints a list of commits with links to the commits themselves. This works great so far, I get a list of commits, when I clock on a link I listen for the route event in a Commits component and change the template that is shown to:

  1.     events: {
          "/:owner/:repo/:commit route": (data) ->
            $('#app').html(can.view('views/commit.mustache', {}))
        }

How do I load the details for a single commit? Is it best to add this to the app state similar to the commits definition? Do I call the findOne function manually, and how do I return the correct value?

  1.     commitDetails: {
          get: () ->
            Commit.findOne {
              owner: this.attr('owner')
              repo: this.attr('repo')
              commit: this.attr('commit')
            }
        }

I'm sure there is a simple answer for this, but I haven't found any example and from the docs I can't see the best way to do this. Thanks in advance :).


Viewing all articles
Browse latest Browse all 3491

Trending Articles