简体   繁体   English

如果尚未存储,如何从服务器获取数据

[英]how to get data from server if not already in store

I have a route in my React application that I can enter from two different directions with different implications. 我在React应用程序中有一条路线,可以从两个不同的方向输入,具有不同的含义。

On my Homepage of the app, I have a list of blog posts. 在该应用程序的主页上,我有一个博客文章列表。 If I click a link to a particular blog post (the first way of entering this route), the BlogPost component can get the blog post data from the BlogPostStore via an id that was populated when the HomePage loaded (and loaded all the blog posts into the store). 如果我单击指向特定博客文章的链接(输入此路线的第一种方法),则BlogPost组件可以通过加载HomePage(并将所有博客文章加载到其中)填充的ID从BlogPostStore获取博客文章数据。商店)。 However, I can also enter that same route from the admin page of the blog (which isn't a React app) and thereby bypass the HomePage, which means the blog post is not in the store. 但是,我也可以从博客的管理页面(不是React应用程序)输入相同的路由,从而绕过主页,这意味着博客文章不在商店中。 In fact, the store is empty if I enter the route that way. 实际上,如果我以这种方式输入路线,那么商店是空的。

If I enter the route the first way, the store can return the blog post without a problem like this, by simply returning the post 如果我以第一种方式输入路线,那么商店只需返回帖子即可返回博客帖子,而不会出现这样的问题

 getPost: function(id){
    return _posts[id];
 }

However, if to account for the second way of entering the route where the store isn't populated, I need to do something like this, [which has obvious problems] created by using a return statement with an ajax request 1 但是,如果要考虑进入不填充商店的路线的第二种输入方式,则需要执行类似的操作([存在明显的问题]),该操作是通过使用带有ajax请求的return语句创建的1

 getPost: function(id){
    if (!_posts[id]){
      /// ajax request
    }
    return _posts[id];
 }

Question: How can I restructure the getPost function of the BlogStore to return an post when it's in the store, but then fall back to an ajax request when it's not in the store? 问题:如何重组BlogStore的getPost函数以使其在商店中时返回一个帖子,但是当不在商店中时又回退到ajax请求?

Note, I do not consider this a duplicate of the question I linked to because in my question, I am first trying to retrieve an item from a store (which necessitates returning it), before falling back to an ajax request if the store isn't populated. 请注意,我不认为这是我链接的问题的重复,因为在我的问题中,我首先尝试从商店中检索商品(必须将其退还),如果商店不是,则回落到ajax请求之前t人口稠密。

Code: 码:

BlogPost component

    getInitialState(){
         return this.getStateFromStore()

    },
    getStateFromStore(props){
        return {
            post:  BlogStore.getPost(id)
         }
    },
    render: function(){
        return (
             <div> {post.title} </div>
         )
    }

The BlogStore

     _posts = {}
     init(){
        getJSON(API, function(err, res){
             res.forEach(function(post){
              _posts[post.id] = post;
             }
        }
     }
     getPosts: function(){ 
     },
     getPost: function(id){
        return _posts[id];
     }


function getJSON(url, cb){
    var req = new XMLHTTPRequest()
    req.onload = function(){
        if(req.status === 404){
           cb(new Error('not found');
         }else{
           cb(null, JSON.parse(req.response);
         }
    }
    req.open('GET', url)
    req.setRequestHeader('authorization', localStorage.token)
    req.send()
}

Note that this.emit('gotPosts') would be in whatever flavor your store supports. 请注意,this.emit('gotPosts')的存储方式将与您的商店支持的口味相同。

 The BlogStore _posts = {} init(){ this.getPosts(); } getPosts: function(){ getJSON(API, function(err, res){ res.forEach(function(post){ _posts[post.id] = post; } this.emit('gotPosts'); } }, getPost: function(id){ var returnPost = _posts[id]; if (returnPost == null) this.getPosts(); return returnPost; } function getJSON(url, cb){ var req = new XMLHTTPRequest() req.onload = function(){ if(req.status === 404){ cb(new Error('not found'); }else{ cb(null, JSON.parse(req.response); } } req.open('GET', url) req.setRequestHeader('authorization', localStorage.token) req.send() } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM