【问题标题】:Netflix Falcor: Filters in model's get operationNetflix Falcor:模型获取操作中的过滤器
【发布时间】:2016-10-06 12:05:15
【问题描述】:

我们正在尝试将Netflix's Falcor 集成到我们的系统中。 我们如何根据过滤器检索路径。例如,如果我们想要获取具有 done: false 值的 Todos。

JSON 图表示例:

{
    todosById: {
        "44": {
            name: "Login to stackoverflow",
            done: true,
            prerequisites: []
        },
        "54": {
            name: "Ask question on stackoverflow",
            done: false,
            prerequisites: [{ $type: "ref", value: ["todosById", 54] }]
        },            
        "64": {
            name: "Accept answer",
            done: false,
            prerequisites: []
        }
    },
    todos: [
        { $type: "ref", value: ["todosById", 44] },
        { $type: "ref", value: ["todosById", 54] },
        { $type: "ref", value: ["todosById", 64] }
    ]
}

现在我们只想获取还不是done: true 的待办事项。

在客户端获取所有待办事项和过滤似乎是多余的。我假设我们可能需要使用 call 操作,但我在指南中找不到任何关于如何在 JSON 图中实现函数的示例。 (也不确定是否有更好的方法)

【问题讨论】:

    标签: javascript falcor


    【解决方案1】:

    将您的模型视为包含客户可以要求的所有内容,而不仅仅是存储的数据。这就是你想要的:

    {
        todosByDone: {
            "true": [
                { $type: "ref", value: ["todosById", 44] }
            ],
            "false": [
                { $type: "ref", value: ["todosById", 54] },
                { $type: "ref", value: ["todosById", 64] }
            ]
        },
        todosById: {
            "44": {
                name: "Login to stackoverflow",
                done: true,
                prerequisites: []
            },
            "54": {
                name: "Ask question on stackoverflow",
                done: false,
                prerequisites: [{ $type: "ref", value: ["todosById", 54] }]
            },            
            "64": {
                name: "Accept answer",
                done: false,
                prerequisites: []
            }
        },
        todos: [
            { $type: "ref", value: ["todosById", 44] },
            { $type: "ref", value: ["todosById", 54] },
            { $type: "ref", value: ["todosById", 64] }
        ]
    }
    

    然后您可以像任何其他数组一样请求完成的待办事项:todosByDone.true[0..10]。当然,您可以根据需要设计模型,并且有很多可能性。从客户的角度考虑建模。例如,如果客户端可以执行todos.donetodos.todo(也就是未完成),那就太好了。然后你的模型看起来像:

    {
        todos: {
            done: [
                { $type: "ref", value: ["todos", "byId", 44] }
            ],
            todo: [
                { $type: "ref", value: ["todos", "byId", 54] },
                { $type: "ref", value: ["todos", "byId", 64] }
            ],
            byId: {
                "44": {
                    name: "Login to stackoverflow",
                    done: true,
                    prerequisites: []
                },
                "54": {
                    name: "Ask question on stackoverflow",
                    done: false,
                    prerequisites: [{ $type: "ref", value: ["todos", "byId", 54] }]
                },            
                "64": {
                    name: "Accept answer",
                    done: false,
                    prerequisites: []
                }
            },
            byCreationTime: [
                { $type: "ref", value: ["todos", "byId", 44] },
                { $type: "ref", value: ["todos", "byId", 54] },
                { $type: "ref", value: ["todos", "byId", 64] }
            ]
        },
    
    }
    

    请随意探索this app 以获取一些示例。

    【讨论】:

    • 感谢您的回答。您的解决方案确实解决了我在问题中发布的问题,因此我接受了它。但是,如果您可能有 10 种不同的状态,并且您可能希望显示它们的不同组合,我希望能够一起查看任意数量的状态(例如,只是“完成”或“完成,待定,延迟”等),构建一个包含所有组合的模型似乎太多了。
    • @omerts This question 就是关于这种情况的。
    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 2015-11-18
    • 2013-05-15
    • 2022-08-02
    • 1970-01-01
    相关资源
    最近更新 更多