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

Saving attachment data & retrieving. #169

Closed
hashg opened this issue Jan 27, 2017 · 4 comments
Closed

Saving attachment data & retrieving. #169

hashg opened this issue Jan 27, 2017 · 4 comments

Comments

@hashg
Copy link

hashg commented Jan 27, 2017

I'm trying to attach and retrieve the data back using ember-pouch "attachment". But after saving the data, there are issues reading the attachment data which is returned as "undefined".

empouch

Repo

Issue is reproducing using my repo - empouch-attach

Currently my repo implements :

  1. Save model without any attachments
  2. Add single file to attachment

Code

Using ember-cli-file-picker addon to "Upload files"

.hbs

{{#file-picker fileLoaded="photoAdded" readAs="readAsFile" accept="image/*"}}
  <button type="button" name="button">Click to upload a photo</button>
{{/file-picker}}

route/actions

    photoAdded(file) {
      const profile = this.controller.get('model');
      const selections = Ember.get(profile, 'photo');
      Ember.Logger.debug(file);

      selections.addObject(Ember.Object.create({
        'name' : file.name,
        'content_type' : file.type,
        'data' : file
      }));
      Ember.Logger.debug(profile);
      profile.save();
   }

Scenarios I'm trying to understand :

  1. Save model without any attachments
  2. Add single file to attachment
  3. Add mutiple files to attachment
  4. Edit model & add more files to attachment
  5. Remove file/s from attachment
  6. Retrieve file/s from attachment

I'm stuck @ Retrieve file/s from attachment. I've a doubt my save mechanism scrambles the model data.

[Edit: More info + format]

@srsgores
Copy link

srsgores commented Jan 28, 2017

Hi @hashg.

I have gotten attachments to work. It turns out there is a typo in the readme. In order to get attachments working properly, you have to rename your DS.attr("attachment" to DS.attr("attachments". When using the former, you will see that defaultValue has no effect; but if you use the latter, the defaultValue will work:

import Model from "ember-pouch/model";
const {attr, hasMany, belongsTo} = DS;

export default Model.extend({
    photos: attr("attachments", {defaultValue: []}), // note the "s" here
    // ...
});

To add attachments, the process is as described in the readme (just note the "s" in the DS.attr call). You can call model.get("photos").addObject();. Of note, however: I was not able to save Files directly in the data field; I had to base64-encode the data.

Now, to answer your question regarding the displaying and retrieving said attachments: you can accomplish this with an {{each}} statement:

<figure class="images-container">
	{{#each model.photos as |photo|}}
		<img src="{{photo.data}}" alt="{model.post.name}}">
	{{/each}}
	<figcaption class="post-image-description">
		{{model.post.name}}
	</figcaption>
</figure>

Of note, this works because a base64-encoded string is a valid value for src.

I hope that helps!

@broerse
Copy link
Collaborator

broerse commented Jan 28, 2017

@srsgores Super! Can you make a PR for the readme? I will merge it.

@ntodd
Copy link
Contributor

ntodd commented Jan 31, 2017

@broerse I updated the readme in #170

@broerse
Copy link
Collaborator

broerse commented Jan 31, 2017

@ntodd Thanks

@broerse broerse closed this as completed Jan 31, 2017
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

4 participants