是否可以通过ajax(而不是预先)在类似时间线中动态加载内容

发布于 2024-09-06 17:35:42 字数 840 浏览 10 评论 0原文

我正在使用 javascript 明喻时间线 有一个时间线项目非常大描述字段。我不想让我的初始 json 有效负载数据膨胀,因为只有在以下情况下才需要这些数据: 有人单击时间线项目。

例如,对于这个 JSON 结果:

 {
 'dateTimeFormat': 'iso8601',
 'wikiURL': "http://simile.mit.edu/shelf/",
 'wikiSection': "Simile Cubism Timeline",

 'events' : [

    {'start': '1880',
    'title': 'Test 1a: only start date, no durationEvent',
    'description': 'This is a really loooooooooooooooooooooooong field',
    'image': 'http://images.allposters.com/images/AWI/NR096_b.jpg',
    'link': 'http://www.allposters.com/-sp/Barfusserkirche-1924-Posters_i1116895_.htm'
    },

我希望从 JSON 中一起删除描述字段(或发送 null),并让它通过另一个 ajax 调用按需加载它。

无论如何,在初始加载期间不发送描述字段,当有人单击时间线项目时,它会通过ajax加载描述,当时

我认为这将是一个常见功能,但我找不到它

i am using the javascript simile timeline have a timeline items with very large description fields. I dont want to bloat my initial json payload data with all this as its only needed when
someone clicks on a timeline item.

So for example, on this JSON result:

 {
 'dateTimeFormat': 'iso8601',
 'wikiURL': "http://simile.mit.edu/shelf/",
 'wikiSection': "Simile Cubism Timeline",

 'events' : [

    {'start': '1880',
    'title': 'Test 1a: only start date, no durationEvent',
    'description': 'This is a really loooooooooooooooooooooooong field',
    'image': 'http://images.allposters.com/images/AWI/NR096_b.jpg',
    'link': 'http://www.allposters.com/-sp/Barfusserkirche-1924-Posters_i1116895_.htm'
    },

i would want to remove the description field all together (or send null) from the JSON and have it load it ondemand through another ajax call.

is there anyway to not send the desription field down during the initial load and when someone clicks on a timeline item have it load the description via ajax at that point

I thought this would be a common feature but i can't find it

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

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

发布评论

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

评论(6

颜漓半夏 2024-09-13 17:35:42

我认为您需要做的事情类似于 @dacracot 所建议的,但您可以利用时间线文档中描述的一些处理程序,特别是 onClick 处理程序。所以我想象你做的是这样的:

//save off the default bubble function
var defaultShowBubble = Timeline.OriginalEventPainter.prototype._showBubble;

//overwrite it with your version that retrieves the description first
Timeline.OriginalEventPainter.prototype._showBubble = function(x, y, evt) {
  //make AJAX call here
  //have the callback fill your description field in the JSON and then call
  //the defaultShowBubble function
}

至少有一个部分我还没有回答,那就是如何确定哪个事件被点击,但你可能可以从 evt.getID()< /code>

编辑:哦,另一个棘手的部分可能是如何将描述插入到时间线数据中。我只是对时间轴这个东西不够熟悉,不知道它是如何完成的。

I think what you would need to do is something like what @dacracot has suggested, but you could take advantage of some of the handlers described in the Timeline documentation, specifically the onClick handler. So what I'm imagining you do is this:

//save off the default bubble function
var defaultShowBubble = Timeline.OriginalEventPainter.prototype._showBubble;

//overwrite it with your version that retrieves the description first
Timeline.OriginalEventPainter.prototype._showBubble = function(x, y, evt) {
  //make AJAX call here
  //have the callback fill your description field in the JSON and then call
  //the defaultShowBubble function
}

There's at least one part I haven't answered, which is how to figure out which event was clicked, but you could probably figure it out from evt.getID()

EDIT: Oh the other tricky part might be how to insert the description into the timeline data. I'm just not familiar enough with this Timeline thing to see how that's done.

老娘不死你永远是小三 2024-09-13 17:35:42

所以我想知道您是否可以放置一个脚本来调用描述。

{
 'dateTimeFormat': 'iso8601',
 'wikiURL': "http://simile.mit.edu/shelf/",
 'wikiSection': "Simile Cubism Timeline",

 'events' : [

    {'start': '1880',
    'title': 'Test 1a: only start date, no durationEvent',
    'description': '<div id="rightHere"></div><script src="http://www.allposters.com/js/ajax.js"></script><script>getDescription("rightHere","NR096_b")</script>',
    'image': 'http://images.allposters.com/images/AWI/NR096_b.jpg',
    'link': 'http://www.allposters.com/-sp/Barfusserkirche-1924-Posters_i1116895_.htm'
    },

稍微分解一下...

这是您更新 javascript 中的 insideHTML 的地方:

<div id="rightHere"></div>

这是进行 ajax 调用并更新 innerHTML 的 javascript:

<script src="http://www.allposters.com/js/ajax.js"></script>

最后,这是将正确的描述放入正确位置的 javascript 调用:

<script>getDescription("rightHere","NR096_b")</script>

我承认我还没有尝试过,但这可能是一个开始。

So I wonder if you could place a script call the description.

{
 'dateTimeFormat': 'iso8601',
 'wikiURL': "http://simile.mit.edu/shelf/",
 'wikiSection': "Simile Cubism Timeline",

 'events' : [

    {'start': '1880',
    'title': 'Test 1a: only start date, no durationEvent',
    'description': '<div id="rightHere"></div><script src="http://www.allposters.com/js/ajax.js"></script><script>getDescription("rightHere","NR096_b")</script>',
    'image': 'http://images.allposters.com/images/AWI/NR096_b.jpg',
    'link': 'http://www.allposters.com/-sp/Barfusserkirche-1924-Posters_i1116895_.htm'
    },

Breaking it down a bit...

This is where you would update the innerHTML in you javascript:

<div id="rightHere"></div>

This is the javascript which makes the ajax call and updates the innerHTML:

<script src="http://www.allposters.com/js/ajax.js"></script>

Finally, this is the javascript call to get the right description into the right location:

<script>getDescription("rightHere","NR096_b")</script>

I admit that I haven't tried this, but it may be a start.

弥枳 2024-09-13 17:35:42

我还必须在 ASP.NET MVC 应用程序中执行类似的操作。
就我而言,我必须在页面加载时执行此操作。您也可以在某些条件\事件下执行此操作。

我所做的是,当我的页面加载时,我向我的部分视图控制器发出了 GET 请求。从那里我返回了一个“PartialViewResult”。然后在 UI 中我将其放置在需要渲染的位置。
请注意,在控制器中,有不同的方法来渲染部分视图。
我没有在控制器中对 UI Html 进行硬编码。这不是一个好的做法。我得到的 UI 渲染方式为:

return PartialView("~/UserControls/Search.ascx", model);

基本上,您的视图引擎正在渲染 UI Html。 :)
如果您想查看我的实现,请点击链接: http://www .realestatebazaar.com.bd/buy/property/search

希望有帮助。

I also had to do something like that in an asp.net MVC Application.
In my case i had to do it on a page load. You can do it on some conditions\events too.

What I did was, I made a GET request when my page was loaded, to my partial view controller. From there I returned a "PartialViewResult". Then in the UI I placed it where it needed to be rendered.
Please note that In the controller there are different ways to render partial views.
I did not hard code the UI Html in the controller. That wouldn't be a good practice. I got the UI rendered by:

return PartialView("~/UserControls/Search.ascx", model);

Which is basically your view engine is rendering the UI Html. :)
If you want to have a look at my implementation here is the link: http://www.realestatebazaar.com.bd/buy/property/search

Hope that helps.

滥情哥ㄟ 2024-09-13 17:35:42

这是一个非常酷的解决方案,如果您愿意使用 Jquery,可以使用 AJAX。非常好的结果!

http://tutorialzine.com/2010/01 /高级事件时间线-with-php-css-jquery/

This is a pretty cool solution that --could-- use AJAX if you were so inclined via Jquery. Very nice result!

http://tutorialzine.com/2010/01/advanced-event-timeline-with-php-css-jquery/

゛时过境迁 2024-09-13 17:35:42

我假设您使用的是 PHP,并且在字符串中包含示例 JSON:

//I have the JSON string in $json::
$jsonArr = json_decode($json);
$jsonOput = array();

//move descriptions into a numbered array, (E.G. a JSON [])
foreach($jsonArr['events'] as $a=>$b) {
    $jsonOput[] = $b['description'];
    unset($jsonArr['events'][$a]['description'];
}

//Output the original JSON, without the descriptions
echo json_encode($jsonArr);
//Output the JSON of just the descriptions
echo json_encode($jsonOput);

显然您只会输出免费的描述,或者唯一的描述;取决于要求。

编辑:修复了代码以正确地说 unset() 而不是 unshift(),印刷错误...

编辑2:MXHR(Multipart XmlHttpRequest)涉及创建所有描述的字符串,并用分隔符分隔。

$finalOput = implode('||',$jsonOput);

并请求该长字符串。当它下降时,您可以读取流并通过搜索 || 拆分任何已完成的流。

I'm assuming you're using PHP, and have the sample JSON in a String:

//I have the JSON string in $json::
$jsonArr = json_decode($json);
$jsonOput = array();

//move descriptions into a numbered array, (E.G. a JSON [])
foreach($jsonArr['events'] as $a=>$b) {
    $jsonOput[] = $b['description'];
    unset($jsonArr['events'][$a]['description'];
}

//Output the original JSON, without the descriptions
echo json_encode($jsonArr);
//Output the JSON of just the descriptions
echo json_encode($jsonOput);

Obviously you'd only output the description free, or the only descriptions; depending on what's requested.

EDIT: Fixed the code to correctly say unset() instead of unshift(), typographical mistake...

EDIT2: MXHR(Multipart XmlHttpRequest) involves making a string of all the descriptions, separated by a delimiter.

$finalOput = implode('||',$jsonOput);

And make a request for that long string. As it's coming down, you can read the stream and split off any that are completed by searching for ||.

小嗲 2024-09-13 17:35:42

那将是服务器端问题。您无法更改前端的数据以使结果更小,因为您已经有了结果。

使用不同的调用或添加参数。

That would be a server side issue. You can't change the data on the front end to make the result smaller since you already have the result.

Use a different call or add parameters.

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