Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example only shows garbage #6

Closed
iprognos opened this issue May 23, 2015 · 17 comments
Closed

Example only shows garbage #6

iprognos opened this issue May 23, 2015 · 17 comments

Comments

@iprognos
Copy link

I have implemented the all content example:

{{ $contentfulEntries.total }} items found:

  • {{ entry.sys.id }}

There is only a list of garbage. How do I get some actual content from this?

@jvandemo
Copy link
Owner

@iprognos — Can you post your markup and the result you get?

For example:

<ul contentful-entries>
  <li ng-repeat="entry in $contentfulEntries.items">
    {{ entry.fields.name }}
  </li>
</ul>

loops over all entries in your Contentful space, displaying the name field.

Can you post your JavaScript and HTML? Thanks!

@iprognos
Copy link
Author

Now I have managed to show the Hemingway blog post:

Get all entries

<section contentful-entries="" >
  <p>{{ $contentfulEntries.total }} items found:</p>
  <ul>
    <li ng-repeat="entry in $contentfulEntries.items">
      <pre>{{ entry.fields.createdEntries[0].fields.body }}</pre>
    </li>
  </ul>

It shows the blog post and then 5 empty ones.

This feel a bit wrong. But I am learning.

@iprognos
Copy link
Author

This works:

  • {{ entry.fields.name }}
  • Result:

    Lewis Carroll
    Mike Springer

    @iprognos
    Copy link
    Author

    I have to learn markdown to make code viewable.... Strange that one is working but not the last one.

    @iprognos
    Copy link
    Author

    I am trying to understand what the id are so I can have a clickable list of Blog entries...

    @jvandemo
    Copy link
    Owner

    @iprognos — Angular-contentful provides you with the data that is received from Contentful.

    You can read more about the JSON format right here on the Contentful website: https://www.contentful.com/developers/documentation/content-delivery-api/#json.

    There you can read on system properties (entry.sys.*) and fields (entry.fields.*) of an entry.

    That is how Contentful structures their documents and is not specific to angular-contentful.

    Hope that makes sense? Thanks!

    @iprognos
    Copy link
    Author

    Yes, great!

    I am now strugling to get a drill down view of blog posts.

    I pass the somewhat no seo friendly Id to my details view:

    Then I run your directive:

    {{ $contentfulEntry }}

    And instead of a hardcoded ID I pass it dynamicly:

    {{ $contentfulEntry }}

    But I have not yet figured out the right syntax, I have tried with contentful-entry="{{id}}" and contentful-entry="({id})" and contentful-entry="id" and contentful-entry="{id}".

    The request to contentful is soething like this, you see whats wrong in the URL:
    https://cdn.contentful.com/spaces/1jdsnorf6e9t/entries/%7B%7Bid%7D%7D?access_token=02d0e52b0cfb62cb9fc011cfe849191fd58a9abb285a388afd68720cf9ab1df7

    @iprognos
    Copy link
    Author

    Not able to pass it dynamicly. I have managed to pass the id succesfully to my details view and I have it in the scoope: $scope.id and I can view it in the details page with this: {{id}} .

    But I can not pass in the {{id}} in the contentful-entry handler. I have tried a lot of syntax on the id.

    I am used to pass in id this way: ui-sref=".detail({id: show.name})"

    @jvandemo
    Copy link
    Owner

    @iprognoscontentful-entry only accepts a string indeed. I can make it dynamic but that would introduce a breaking change.

    If I were you I would resolve the entry in the route's resolve using the contentful service:

    // add resolve in your UI-router route:
    resolve: {
      entry: getEntry
    }
    
    function getEntry($stateParams, contentful){
      return contentful
        .entry($stateParams.id)
        .then(function(response){
          return response.data;
        });
    }
    
    getEntry.$inject = ['$stateParams', 'contentful'];
    

    Then you get the entry injected in your controller and you can:

    function YourController($scope, entry){
      $scope.entry = entry;
    }
    

    to make the entry accessible in your view.

    I'm typing this out of my head. If you can create a plunker I am happy to help you out there.

    Hope that helps!

    @iprognos
    Copy link
    Author

    Yes that could be a way to do it.

    I did it this way:

    I have this in my ui-router:

    .state('app.blog.detail', {
    url: '/:id',
    views: {
    'content@': {
    templateUrl: 'app/templates/partials/blog-detail.html',
    controller: 'blogDetailsController'
    }
    }
    });

    I have a blogDetailsController:

    app.controller('blogDetailsController', ['$scope','$stateParams', 'contentful', function($scope, $stateParams, contentful ) {
    contentful
    .entry($stateParams.id)
    .then(
    function (response) {
    $scope.$contentfulEntry = response.data;
    console.log($scope.$contentfulEntry);
    },
    function(){
    $scope.$contentfulEntry = {};
    });
    }]);

    And in my blog-detail.html I have:

    Get one entry

    {{ $contentfulEntry }}


    ID: {{id}}

    The problem now is that I can't get the body from the blog post:

    {{ $contentfulEntry.fields.createdEntries[0].fields.body }}

    Its not there.

    I have this in the $contentfulEntry:

    {"name":"Lewis Carroll","website":"http://en.wikipedia.org/wiki/Lewis_Carroll","profilePhoto":{"sys":{"id":"2ReMHJhXoAcy4AyamgsgwQ","type":"Link","linkType":"Asset"}},"biography":"Charles Lutwidge Dodgson (27 January 1832 – 14 January 1898), better known by his pen name, Lewis Carroll, was an English writer, mathematician, logician, Anglican deacon and photographer. \n\nHis most famous writings are Alice's Adventures in Wonderland, its sequel Through the Looking-Glass, which includes the poem Jabberwocky, and the poem The Hunting of the Snark, all examples of the genre of literary nonsense. \n\nHe is noted for his facility at word play, logic, and fantasy. There are societies in many parts of the world (including the United Kingdom, Japan, the United States, and New Zealand[3]) dedicated to the enjoyment and promotion of his works and the investigation of his life. In 1982, his great-nephew unveiled his memorial stone in Poets' Corner, Westminster Abbey.","createdEntries":[{"sys":{"id":"1asN98Ph3mUiCYIYiiqwko","type":"Link","linkType":"Entry"}}]}

    @jvandemo
    Copy link
    Owner

    @iprognos — Ah, if your entry has links to other entries, you should resolve them.

    However, if you use contentful.entries(querystring) the resolution will be done automatically for you.

    So can you try to use something like contentful.entries('sys.id=id-of-your-entry') instead of contentful.entry(id)?

    Again I'm typing this without access to the docs so make sure to check the actual syntax. Thx!

    @iprognos
    Copy link
    Author

    Ah ok, I will try that. I did not know the syntax for the query string so I avoided it.

    @iprognos
    Copy link
    Author

    Puh.. Got it working:

    blogdetails.controler.js:

    app.controller('blogDetailsController', ['$scope','$stateParams', 'contentful', function($scope, $stateParams, contentful ) {

        contentful
        .entries('sys.id=' + $stateParams.id )
        .then(
          function (response) {
            $scope.$contentfulEntries = response.data;
              console.log('Response');
              console.log($scope.$contentfulEntries);
          },
          function(){
            $scope.$contentfulEntries = {
              limit: 0,
              skip: 0,
              total: 0,
              items: []
            };
          }
        );
    
    }]);
    

    blogdetail.html:

    Directives demo

    This simple demo uses the following Contentful configuration:

    Get one entry

    {{ $contentfulEntries.items[0].fields.createdEntries[0].fields.body}}

    @iprognos
    Copy link
    Author

    Is it possible to get an entry by slug instead of ID, it´s not very SEO friendly to have a ID in the URL?

    I searched the API documentation and could not find any comprehensible. Then I tried with: .entries('fields.slug=down-the-rabbit-hole')

    but of course it did not work.

    @jvandemo
    Copy link
    Owner

    @iprognos — Yes, you can specify a query string like:

    content_type=cat&fields.likes=lasagna

    You can see the exact documentation right here.

    Let me know if it works, ok?

    @iprognos
    Copy link
    Author

    No does not work. query=all works best, then I get three entries and avoid listing categories and authors.

    @jvandemo
    Copy link
    Owner

    Closed, continued in #7.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants