好主意/扩展 Spine.Model 的最佳方式

发布于 2024-12-23 15:38:17 字数 692 浏览 2 评论 0原文

[背景如下] 我在后端用 Python 的 SQLObject 建模了数据。现在,我正在将 SQLObject 转换为字典,并从字典中获取所有键,然后将其导出为 JSON 文档(因此只是一个 JavaScript 数组)。我打算做一些类似的事情:

Spine.Model.extend({
    fromList: function(name, list){
        var model = Spine.Model.setup(name, list);
        return model;
    }
});

这是个好主意吗? Spine已经提供这个功能了吗?这是扩展 Spine.Model 类的最佳方法吗?

背景:

所以。我有一个 Python 应用程序,我已使用 Flask 将其从 GUI 应用程序移植到 Web 应用程序。

我现在正在做视图部分,并意识到使用 JavaScript 框架来操作数据/控制应用程序等会很有意义。

经过大量研究后,我选择了 Spine(第一次阅读时,该 API 对我来说最有意义,而且作者写了 O'Reilly 的书《JavaScript Web 应用程序》,因此有一个不错的参考)。

由于我已经在后端对数据进行了建模,因此我想导出该配置并使用它自动创建 Spine 模型,以便它们记录的数据始终保持同步(这样,如果我更改我的后端模型,前端在下一页加载时自动更改)。

[background below]
I've got my data modelled out in SQLObject in Python on the back-end. Right now I'm converting an SQLObject to a dict, and grabbing all the keys from the dict and then exporting that as a JSON document (so just a JavaScript array). I was planning on doing something like:

Spine.Model.extend({
    fromList: function(name, list){
        var model = Spine.Model.setup(name, list);
        return model;
    }
});

Is this a good idea? Does Spine already provide this functionality? Is this the best way to extend the Spine.Model class?

BACKGROUND:

So. I've got a Python application that I've been porting from a GUI app to a web app using Flask.

I'm to the point where I'm doing the view part and realized that it would make a lot of sense to use a JavaScript framework for manipulation of the data/controlling the app/etc.

After a bunch of research I've settled on Spine (the API made the most sense to me on the first read, plus the author wrote the O'Reilly book JavaScript Web Applications so there's a decent reference).

Since I've already got my data modelled out on the backend, I'd like to export that configuration and automate the creation of the Spine models using this so that the data they're recording is always in sync (this way if I change my back-end model, the front-end automatically changes at next page load).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2024-12-30 15:38:17

看起来您正在考虑使用客户端 JavaScript 基于数据库中的模型和 python 字典动态创建模型 -> JSON 作为两者之间的链接表示。

这听起来很复杂,我真的没有答案。它甚至可能不必要地复杂:),但这由您决定。不过,我确实有一个替代解决方案。

为什么不从 Python 动态生成 Spine 模型并只提供静态文件?然后,您所要做的就是编写一个 python 程序,用 JavaScript 或 CoffeeScript 输出脊柱模型的有效代码(如果模型在开发过程中经常更改或只是根据需要更改,则可能作为构建过程的一部分)。

同样,如果您没有大量需要定期更改的模型,这可能会变得不必要的复杂。即使如此,也许您所需要的只是一个验证工具,用于验证您的后端数据是否在 Spine 中正确建模,并且只需手动编写所有内容即可。手动编写模型代码以包含所需的数据是相当容易的http://spinejs.com/docs/models

实际上,在脊柱模型中设置实际的“数据”就像@configure "Contact", "first_name", "last_name" 现在脊柱模型有一个名字和一个姓氏。 ..

确保你在你想出这个主意之前,我并没有戴上你的Complicator 手套:)

It looks like you are thinking about dynamically creating models with client side JavaScript based on a model in your database with a python dictionary -> JSON as the linking representation between the two.

This sounds complicated and I really don't have an answer. It may even be unnecessarily complicated :), but that's for you to decide. I do however, have an alternative solution.

Why not generate the Spine models from Python dynamically and just serve the static files? Then all you have to do is write a python program that outputs valid code for a spine model in JavaScript or CoffeeScript (perhaps as part of your build process if models change often or simply as necessary during development).

Again, this may be unnecessarily complicated if you do not have a LARGE amount of models, that change regularly to generate. Even then perhaps all you need is a verification tool that verifies your backend data is modeled correctly in Spine, and just hand code everything. It's fairly easy to hand code the models to contain the data they need http://spinejs.com/docs/models

Really, setting up the actual "data" in the spine model is as simple as @configure "Contact", "first_name", "last_name" Now the spine model has a first_name and a last_name...

Make sure you didn't put on your Complicator Gloves, before you came up with this idea :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文