简体   繁体   English

在Raphael.js中使用Ember.js:如何将Ember对象与Raphael对象相关联?

[英]Using Ember.js with Raphael.js: How to associate Ember objects with Raphael objects?

The application that I'm building uses Ember.js and Raphael.js. 我正在构建的应用程序使用Ember.js和Raphael.js。 Since I'm new to Ember.js, I'm having some issues understanding how to tie the two together. 由于我是Ember.js的新手,我在理解如何将两者结合在一起时遇到了一些问题。 I have already seen this demo , but that only gets me part of the way. 我已经看过这个演示 ,但这只是让我的一部分。

Let me phrase my question this way: 让我以这种方式说出我的问题:

Let's take the "todo" demo from Ember.js's documentation as an example. 我们以Ember.js文档中“todo”演示为例。 How could I associate an image with each todo item, and have that image displayed on a Raphael drawing canvas when the todo item is not completed? 如何将图像与每个待办事项相关联,并在待办事项未完成时将该图像显示在Raphael绘图画布上? To clarify, there is only 1 Raphael drawing surface where all of the images are displayed. 为了澄清,只有一个拉斐尔绘图表面显示所有图像。

Just like in the Ember.js demo, I'd like to use fixtures for my todo information, but I'd like to add an image field: 就像在Ember.js演示中一样,我想使用灯具来获取我的待办事项信息,但我想添加一个图像字段:

Todos.Todo.FIXTURES = [
  {
    id: 1,
    title: 'Learn Ember.js',
    isCompleted: true,
    image: 'some.png'
  },
  {
    id: 2,
    title: '...',
    isCompleted: false,
    image: 'some_other.png'
  }
];

Please be patient with me since I haven't had much exposure to Ember.js. 请耐心等待,因为我没有接触过Ember.js。 I need to know 我需要知道

  1. Where and how to initialize the Raphael drawing surface 在哪里以及如何初始化拉斐尔绘图表面
  2. How to add the images to the drawing surface, 如何将图像添加到绘图表面,
  3. How to toggle a image's visibility based on the associated task's isCompleted value. 如何根据关联任务的isCompleted值切换图像的可见性。

Whatever object in which you're storing the image completion data, you need to have an observer function that responds to changes in the completion status. 无论您存储图像完成数据的对象是什么,您都需要具有响应完成状态更改的观察器功能。 In that function, you would do whatever is neccessary to add/remove the image from raphael. 在该函数中,您可以执行任何必要的操作来添加/删除raphael中的图像。 (sorry, I don't know raphael so couldn't tell you exactly what that function contains). (对不起,我不知道raphael因此无法确切地告诉你该函数包含的内容)。

The observer would get called each time to Object.set('imageCompleted',true/false); 观察者每次都会被调用到Object.set('imageCompleted',true / false);

Example: 例:

  App.RaphaelPainter = Ember.Object.extend({
    imageCompleted:null,
    paintOnRaphaelCanvas:function(){
      if(this.get('imageCompleted')){
         // erase the image from raphael
      }
      else {
         // add the image to raphael
      }
    }.observes('imageCompleted')
  });

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

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