2017-08-09 44 views
0

我有一個重複的卡型自定義元素綁定到從api拉出的JSON數據。我目前創建數據並將其綁定到這些卡片上的方法是有效的,但所有與它有關的信息都尖叫着「摺疊」。 Aurelia有沒有更好的方法來實現這些目標?Aurelia,自定義元素上的長長的可綁定列表。重構?

我現在的自定義元素的圖案看起來像這樣

尋呼機view.html

<!-- Loop through the JSON task-data (each card contains the chosen task-data to display) --> 
    <template repeat.for="task of pageData[currentPage - 1]"> 

    <!--Bind each tasks data to a card as we loop--> 
    <card-commit 

     task-data.bind="task" 
     task-id.bind="task.ID" 
     task-name.bind="task.name" 
     project-id.bind="task.project.ID" 
     project-name.bind="task.project.name" 
     assigned-to.bind="task.assignedTo.name" 
     successors.bind="task.successors" 
     commit-status.bind="task.commitStatus" 
     planned-start-date.bind="task.plannedStartDate" 
     planned-comp-date.bind="task.plannedCompletionDate" 
     duration.bind="task.duration" 
     actual-start-date.bind="task.actualStartDate" 
     commit-date.bind="task.commitDate" 
     condition.bind="task.condition" 
     constraint.bind="task.taskConstraint" 
     constraint-date.bind="task.constraintDate" 
     status.bind="task.status" 
     note.bind="task.lastNote" 
     note-text.bind="task.lastNote.noteText" 
     note-entry-date.bind="task.lastNote.entryDate" 
     note-avatar-download-url.bind="task.lastNote.owner.avatarDownloadURL" 
     note-owner-name.bind="task.lastNote.owner.name" 
     actual-completion-date.bind="task.actualCompletionDate" 
    ></card-commit> 

    </template> 

卡commit.ts

import {bindable, inject} from 'aurelia-framework'; 

export class CardCommit { 
    @bindable public taskData; 
    @bindable public taskId; 
    @bindable public taskName; 
    @bindable public projectId; 
    @bindable public projectName; 
    @bindable public assignedTo; 
    @bindable public successors; 
    @bindable public commitStatus; 
    @bindable public plannedStartDate; 
    @bindable public plannedCompDate; 
    @bindable public duration; 
    @bindable public actualStartDate; 
    @bindable public actualStartDelta; 
    @bindable public commitDate; 
    @bindable public condition; 
    @bindable public conditionClass; 
    @bindable public conditionText; 
    @bindable public constraint; 
    @bindable public constraintDate; 
    @bindable public status; 
    @bindable public note; 
    @bindable public noteText; 
    @bindable public noteEntryDate; 
    @bindable public noteAvatarDownloadUrl; 
    @bindable public noteOwnerName; 
    @bindable public updateNoteText; 
    @bindable public actualCompletionDate; 

    constructor() { 
    // ... do constructor stuff 
    } 

    // ... other methods etc 
} 

卡commit.html

<!-- Do all sorts of stuff with the bound data, for example... --> 
<template> 
    <!-- This example wouldn't really work, just demonstrating how I'm using the bound data --> 
    <article data-task-id="${ taskId }"> 
    <ul repeat.bind="for example of task"> 
     <li data-example-commit="example.commitDate">${example.condition}</li> 
    </ul> 
    </article> 
</template> 

也許我過於挑剔的,但如果感覺我應該能夠定義至少這種關係更緊湊的方式,特別是被定義的一長串綁定(本質)的兩倍。但我不確定我還能如何實現這一目標,並且無法真正找到有關該主題/問題的更多信息。

+0

這些屬性是否獨立地改變,或者它們總是一次性改變爲一個組? –

+0

@AshleyGrant:如果我正確理解你的問題,他們會立即改變。上述綁定的目的主要是顯示。但是,顯示的重點是讓用戶獨立更改某些屬性並將其提交回API。 – DjH

+0

如果它們全都改變,那麼你可以只綁定一個單獨的屬性,它是具有所有這些屬性的對象。然後當你需要改變所有的屬性,只需綁定一個全新的對象。 –

回答

0
  • 您可以您剛纔綁定的對象的類屬性(就像我在pager-view.htmltask-data.bind="task"一樣)

  • 使用這個對象引用您可以直接在模板中想要的東西。

因此,修訂後的會是這樣的......

尋呼機view.html

<!-- Loop through the JSON task-data (each card contains the chosen task-data to display) --> 
    <template repeat.for="task of pageData[currentPage - 1]"> 

    <!--Bind each tasks data to a card as we loop--> 
    <card-commit 
     task-data.bind="task" 
    ></card-commit> 

    </template> 

卡commit.ts

import {bindable, inject} from 'aurelia-framework'; 

export class CardCommit { 
    @bindable public taskData; 

    constructor() { 
    // ... do constructor stuff 
    } 

    // ... other methods etc 
} 

card-commit.ht毫升

<!-- Now we reference the data that we want directly on the `taskData` attribute --> 
<!-- Do all sorts of stuff with the bound data, for example... --> 
<template> 
    <!-- This example wouldn't really work, just demonstrating how I'm using the bound data --> 
    <article data-task-id="${ taskData.taskId }"> 
    <ul repeat.bind="for example of taskData.task"> 
     <li data-example-commit="example.commitDate">${example.condition}</li> 
    </ul> 
    </article> 
</template> 

然而,(也許這是可能的,但我找不到它的任何引用),這將是真的高興能夠注入/傳遞taskData到在類的構造,有點像這樣...

尋呼機view.html

<!--Bind each tasks data to a card as we loop--> 
<card-commit 
    card-commit.constructor="task" 
></card-commit> 

然後能夠將它合併到類的構造函數中,可能是這樣的:

卡提交。TS

import {bindable, inject} from 'aurelia-framework'; 

export class CardCommit { 
    @bindable public taskData; 

    constructor(taskData) { 
    Object.entries(taskData).forEach(([key, val]) => { 
     Object.assign(this, {[key]: val}); 
    }); 
    } 

    // ... other methods etc 
} 

然後我們可以在提出了重構代碼初始直接訪問屬性的對象,等等。

如果我有時間找到一種方法來完成此操作,我會更新。同時,如果其他人知道如何去做,我很樂意接受這個答案來代替這個。

+0

使用with binding來更改子項的數據上下文。這樣你就可以在你的card-commit.html, 中獲得最小的重構,參見http://patrickwalters.net/aurelia-data-binding-strategies/ –

相關問題