使用自定义用户生成的内容保存可拖动元素的位置

发布于 2024-10-28 14:46:12 字数 626 浏览 9 评论 0原文

如果我需要存储动态创建的内容,当插入到页面中时,将在容器内有一个预定义的位置,那么更好的路径是什么?

为了更容易理解一点,假设用户在输入框中输入了一些内容,结果是一个 YouTube URL。一旦他/她按下 Enter 键,我就会获取 URL 并创建一个嵌入视频,并将其添加到类名为 box 的容器内的页面中。 这是动态内容

该框也可以使用 jQuery-ui 进行拖动,因此位置不会始终相同。现在,如果我要重新加载页面,我需要页面看起来与重新加载之前完全相同,包括位置和用户动态创建的内容。

我知道可能有多种方法可以做到这一点,但有限制。因为没有可用的服务器来存储这些信息,所以我只能使用:

  • localStorage
  • Web SQLite

还值得一提的是,这将在 Google Chrome 扩展中使用。

所以我的问题是,鉴于限制,存储此类信息的最佳方式是什么?

我在这里使用的示例:http://fiddle.jshell.net/Shaz/pfs8a/30/显示/灯光/

What would be the better path to take if I were in need of storing dynamically created content, which when inserted into the page, will have a pre-defined position within a container?

To make it a little more simpler to understand, say a user types something into an input box, which turns out to be a YouTube URL. Once he/she hits enter I take the URL and create an embedded video and add it to the page within a container of class name box. This is the dynamic content.

This box can also be dragged using jQuery-ui, so the position will not always be the same. Now if I were to reload the page, I need the page to look exactly as it did before it reloaded, including the position and dynamically created content by the user.

I understand there are probably a number of ways to do such a thing, but there are limitations. Because there is no server available to store this information with, I am limited to using:

  • localStorage
  • Web SQLite

It's also worth mentioning that this will be used in a Google Chrome Extension.

So my question is, what would be the best way to store this kind of information given the limitations?

Example with what I'm working with here: http://fiddle.jshell.net/Shaz/pfs8a/30/show/light/

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

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

发布评论

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

评论(1

冧九 2024-11-04 14:46:12
function contentHash(domNode) {
    ... // return unique id based on content / dom node.
}

var myUniqueSalt = ... // unique salt for your unique id's

window.localStorage.setItem(uniqueSalt + contentHash(domNode), "{'x':" + x + ",'y':" + y "}");

...

var position = JSON.parse(window.localStorage.getItem(uniqueSalt + contentHash(domNode));

object.x = position.x;
object.y = position.y;

沿着这些思路的东西应该足够了。

您需要一个唯一的盐来确保没有人覆盖您在 localStorage 中的位置,然后您希望从存储该位置的内容中获取一个唯一的 ID。

只需将位置存储为 JSON 字符串,然后使用 JSON.parse 获取位置对象即可。

当然,检查 getItem 是否返回空字符串,如果是,则使用默认值。

我会进一步建议您使用backbone.js来存储数据并将其连接到 backbone.js-本地存储。它会覆盖backbone.sync方法,以便模型保存在本地。

这意味着您不必将数据序列化到本地存储中,也不必将其从本地存储中序列化。

function contentHash(domNode) {
    ... // return unique id based on content / dom node.
}

var myUniqueSalt = ... // unique salt for your unique id's

window.localStorage.setItem(uniqueSalt + contentHash(domNode), "{'x':" + x + ",'y':" + y "}");

...

var position = JSON.parse(window.localStorage.getItem(uniqueSalt + contentHash(domNode));

object.x = position.x;
object.y = position.y;

Something along those lines should suffice.

You want a unique salt to ensure no-one overwrites your location in localStorage and then you want to get a unique id from your content that your storing the position.

Simply store the position as a JSON string then use JSON.parse to get your position object later.

of course check for whether the getItem returns an empty string and if so use the default.

I'll go even further to recommend you use backbone.js to store your data and connect it upto backbone.js-localstorage. Which overwrites the backbone.sync method so the models are saved locally.

This means you don't have to serialize your data into localstorage nor serialize it out of local storage.

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