How do you invalidate a whole range in Falcor?

376 views Asked by At

Suppose I have a simple system of categories and articles, and there's a path like this to get a list of all the articles in a specific category.

categoriesById[{integers:ids}].articles[{ranges:rows}]

Now, that route is hard-coded to sort the articles by newest first, so when I add a new article, I need to (I assume) invalidate all the articles within a category because I want article 0 to be the new article.

Is there a short-hand way to invalidate the entire range of articles within a category, or do I need to invalidate every article under categoriesById explicitly, or, is there a way to splice an item into a list in the JSONGraph?

2

There are 2 answers

4
Hugo Wood On BEST ANSWER

You can use a range object.

call(path, arg, refPaths, thisPaths) {
    // add new article
    const newLength = // get new length somehow
    return [
        {
            path: [
                "categoriesById", 
                path.categoryId, 
                "articles",
                {length: newLength}
            ],
            invalidated: true
        }
    ]
}

Note that you may want your call tu return more information about the newly created article or the updated number of articles. Here it returns nothing but the fact that the entire articles array must be erased from the client's cache. To return more information, you can add path/value pairs to the returned array.

1
Hugo Wood On

You can give the path to the root of the collection to invalidate.

var model = new falcor.Model({
  cache: {users: [{name: "Scott"}]}
})

console.log(model.getCache().users) // Object {0: Object, length: Object}
model.invalidate("users")
console.log(model.getCache().users) // undefined

Fiddle