Netflix / falcor

A JavaScript library for efficient data fetching
http://netflix.github.io/falcor
Apache License 2.0
10.49k stars 446 forks source link

Returning empty object when getting branch nodes (useful for decoupling deref) #951

Open augonis opened 5 years ago

augonis commented 5 years ago

An example to illustrate.

var model = new falcor.Model({
  cache: {
    todos: {
      a:{
        name: 'get milk from corner store',
        done: false
      },
    }
  }});

model.get('todos.a').then(j => console.log(JSON.stringify(j, null, 2)))

//prints
{
  "json": {
    "todos": {
      "$__path": ["todos"]
    }
  }
}

There's no mention of the path I asked for (implying it doesn't exist?) Instead I propose to make the response be

{
  "json": {
    "todos": {
      "$__path": ["todos"]
      "a": {
        "$__path": ["todos", "a"]
      }
    }
  }
}

It includes the requested path, indicating that there is something beyond there and giving its optimized path ripe for model.deref(). This is in line with Falcors philosophy of only returning what the client asked for.

This change would be most useful for a controller handing out dereferenced models to views. One can imagine a generic list view that (itself been given a model dereferenced to an array) creating child views with models dereferenced to individual array items. Currently it is impossible to dereference any item without knowledge of some leaf value within (or beyond) the item, preventing the creation of such truly generic list view.

I don't see this change would break something, but I might be wrong.